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
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
constexpr ll MAXN = 7e3+4;
constexpr ll INF = 1e18+7;

ll dp[MAXN][MAXN], odp[MAXN];
vector<pii> zel[MAXN];

void licz(ll k, ll m) {
  for(ll i = 1; i <= k; ++i) {
    for(auto &[c, w] : zel[i]) {
      for(ll x = 0; x < m; ++x) {
        dp[i][(w+x)%m] = min(dp[i-1][x]+c, dp[i][(w+x)%m]);
      }
    }
  }
}

void gen(ll k, ll m) {
  for(ll i = 1; i < m; ++i) {
    ll id = i;
    ll pp = dp[k][id];
    
    if(pp == INF) continue;
    while(id%m && pp > 0) {

      dp[k][(id%m)] = min(pp, dp[k][(id%m)]);
      pp += dp[k][i];
      id += i;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, k, m;
  cin >> n >> k >> m;
  for(ll i = 0; i < MAXN; ++i) for(ll j = 0; j < MAXN; ++j) dp[i][j] = INF; 
  dp[0][0] = 0;
  for(ll i = 1; i <= n; ++i) {
    ll id, w, c;
    cin >> id >> w >> c;
    zel[id].emplace_back(c, w);
  }
  licz(k, m);
  gen(k, m);
  vector<pii> pl;
  for(ll i = 1; i < m; ++i) if(dp[k][i] != INF || dp[k][i] < 0) pl.emplace_back(i, dp[k][i]);
  for(ll i = 1; i < MAXN; ++i) odp[i] = INF;
  for(auto &[w, c] : pl) {
    for(ll x = 0; x < m; ++x) {

      odp[(w+x)%m] = min(odp[(w+x)%m], odp[x]+c);
    
    }
  }
  for(ll i = 0; i < m; ++i) cout << ((odp[i] != INF) ? odp[i] : -1) << '\n';
}