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
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e18+7;
const int LIM=7e3+7;
vector<pair<ll,ll>>V[LIM], A, B, C;
ll dp1[LIM][LIM], dp[LIM];
int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  ll n, k, m;
  cin >> n >> k >> m;
  rep(i, n) {
    ll a, b, c;
    cin >> a >> b >> c; --a;
    V[a].pb({b, c});
  }
  rep(i, k+1) rep(j, m) dp1[i][j]=INF;
  dp1[0][0]=0;
  rep(i, k) {
    for(auto j : V[i]) {
      rep(l, m) dp1[i+1][(l+j.st)%m]=min(dp1[i+1][(l+j.st)%m], dp1[i][l]+j.nd);
    }
  }
  rep(i, m-1) dp[i+1]=INF;
  rep(i, m) A.pb({dp[i], i});
  rep(i, m) {
    ll c=dp1[k][i];
    if(c==INF) continue;
    ll l1=0, l2=0;
    while(l1<A.size()) {
      ll x=A[l1].nd;
      if(l2<B.size() && B[l2].st<A[l1].st) {
        ++l2;
        if(dp[B[l2-1].nd]<B[l2-1].st) continue;
        x=B[l2-1].nd;
      } else {
        ++l1;
        if(dp[A[l1-1].nd]<A[l1-1].st) continue;
      }
      C.pb({dp[x], x});
      if(dp[(x+i)%m]>dp[x]+c) {
        dp[(x+i)%m]=dp[x]+c;
        B.pb({dp[(x+i)%m], (x+i)%m});
      }
    }
    A=C;
    B.clear();
    C.clear();
  }
  rep(i, m) if(dp[i]==INF) dp[i]=-1;
  rep(i, m) cout << dp[i] << '\n';
}