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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define st first
#define nd second

const int K = 7005;
const int M = 7005;

map<ll, ll> best_price[K];
vector<pair<ll, ll>> cukierasy[K];

ll plecak[2][M];

vector<pair<ll, ll>> edges;

ll dists[M];

void TEST([[maybe_unused]] int testcase_i) {
  ll n, k, m;
  cin >> n >> k >> m;

  vector<tuple<ll, ll, ll>> v;
  rep(i, n) {
    ll ki, mi, ci;
    cin >> ki >> mi >> ci;
    v.emplace_back(ki, mi, ci);
  }

  for (const auto &[ki, mi, ci] : v) {
    if (best_price[ki][mi] == 0) {
      best_price[ki][mi] = ci;
    } else {
      best_price[ki][mi] = min<ll>(ci, best_price[ki][mi]);
    }
  }

  rep(i, k + 1) {
    for (const auto &[mi, ci] : best_price[i]) {
      cukierasy[i].push_back({mi, ci});
    }
  }

  rep(i, 2) {
    rep(j, M) {
      plecak[i][j] = 1e18;
    }
  }
  plecak[0][0] = 0;

  int prv = 0;
  int cur = 1;
  rep(i, k) {
    rep(j, M) {
      plecak[cur][j] = 1e18;
    }
    for (const auto &[mi, ci] : cukierasy[i + 1]) {
      rep(j, M) {
        plecak[cur][(j + mi) % m] = min(plecak[cur][(j + mi) % m], plecak[prv][j] + ci);
      }
    }
    prv ^= 1;
    cur ^= 1;
  }

  rep(i, M) {
    if (plecak[prv][i] < (ll)1e18) {
      edges.emplace_back(i, plecak[prv][i]);
    }
  }

  rep(i, M) {
    dists[i] = 1e18;
  }
  dists[0] = 0;

  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<>> pq;
  pq.push({0, 0});
  while (pq.size()) {
    auto [dst, w] = pq.top();
    pq.pop();

    if (dists[w] < dst) {
      continue;
    }

    for (const auto &[waga, cena] : edges) {
      int nxt_w = (int)(w + waga) % m;
      if (dists[nxt_w] > dst + cena) {
        dists[nxt_w] = dst + cena;
        pq.push({dst + cena, (w + waga) % m});
      }
    }
  }

  rep(i, m) {
    if (dists[i] > (ll)1e17) {
      cout << -1;
    } else {
      cout << dists[i];
    }
    cout << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  cout << setprecision(20) << fixed;

  // srand(time(NULL));

  int t = 1;
  // cin >> t;
  rep(i, t) {
    TEST(i);
  }

  return 0;
}