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
#include <bits/stdc++.h>
#define ll long long

const int MAX_N = 7e3;
const ll inf = 2e18;

int n, k, m;

ll dp[MAX_N + 3][MAX_N + 3];
int c[MAX_N + 3]; // color
int w[MAX_N + 3]; // weight
ll v[MAX_N + 3];  // value
std::vector<int> ids_with_c[MAX_N + 3];

void input() {
  std::cin >> n >> k >> m;
  for (int i = 1; i <= n; i++) {
    std::cin >> c[i] >> w[i] >> v[i];
    ids_with_c[c[i]].push_back(i);
  }
}

void calc_dp() {
  dp[0][0] = 0;
  for (int r = 1; r <= m - 1; r++)
    dp[0][r] = inf;

  for (int t = 1; t <= k; t++) {
    for (int r = 0; r <= m - 1; r++) {
      dp[t][r] = inf;
      for (auto &i : ids_with_c[t])
        dp[t][r] = std::min(dp[t][r], v[i] + dp[t - 1][(r - w[i] + m) % m]);
    }
  }
}

ll d[MAX_N + 3];
bool processed[MAX_N + 3];

void dij() {
  for (int r = 0; r <= m - 1; r++)
    d[r] = inf;
  d[0] = 0;
  while (1) {
    int next_to_process = -1;
    for (int r = 0; r <= m - 1; r++) {
      if (processed[r])
        continue;
      if (next_to_process == -1 || d[r] < d[next_to_process])
        next_to_process = r;
    }

    if (next_to_process == -1)
      break;

    int v = next_to_process;
    processed[v] = true;

    for (int r = 0; r <= m - 1; r++) {
      if (r == v || processed[r])
        continue;
      int x = (r - v + m) % m;
      if (dp[k][x] < inf && d[v] + dp[k][x] < d[r])
        d[r] = d[v] + dp[k][x];
    }
  }
}

int main() {
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(NULL);

  input();
  calc_dp();
  dij();

  for (int r = 0; r <= m - 1; r++)
    std::cout << ((d[r] >= inf) ? -1 : d[r]) << "\n";
}