1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const ll infll = ((ll)1e18) + 5;

signed main() {
  cin.tie(0) -> sync_with_stdio(0);
  int n, k, m;
  cin >> n >> k >> m;
  
  vector<tii> v(n);
  for(auto &[color, mass, price] : v) cin >> color >> mass >> price;
  
  sort(all(v));
  
  vector<ll> rucs[2];
  rucs[0].assign(m, infll);
  rucs[1].assign(m, infll);
  rucs[0][0] = 0;
  
  int lastcol = 1;
  
  for(auto &[color, mass, price] : v) {
    if(color - lastcol > 1) {
      cout << "0\n";
      for(int i = 1; i < m; i++) cout << "-1\n";
      exit(0);
    }
    
    //cerr << color << ' ' << mass << ' ' << price << '\t' << lastcol << '\n';
    
    if(color > lastcol) {
      swap(rucs[0], rucs[1]);
      rucs[1].assign(m, infll);
    }
    for(int i = 0; i < m; i++)
      rucs[1][(i + mass) % m] = min(rucs[1][(i + mass) % m], rucs[0][i] + price);  
    
    lastcol = color;
  }
  
  if(lastcol != k) {
    cout << "0\n";
    for(int i = 1; i < m; i++) cout << "-1\n";
    exit(0);
  }

  //for(auto x : rucs[1])
    //cerr << x << ' ';
  //cerr << '\n';
  
  vector<ll> rez;
  rez.assign(m, infll);
  rez[0] = 0;
  
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> heap;
  heap.emplace(0, 0);
  while(!heap.empty()) {
    auto [C, p] = heap.top();
    heap.pop();
    if(C != rez[p]) continue;
    for(int j = 1; j < m; j++) {
      if(rez[(p + j) % m] > rez[p] + rucs[1][j]) {
        rez[(p + j) % m] = rez[p] + rucs[1][j];
        heap.emplace(rez[p] + rucs[1][j], (p + j) % m);
      }
    }
  }
  
  for(auto x : rez)
    cout << (x == infll? -1 : x) << '\n';
}

/**
  Anul asta nu se da centroid
  -- Rugaciunile mele
*/