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
#if defined(EMBE_DEBUG) && !defined(NDEBUG)
#include "embe-debug.hpp"
#else
#define LOG_INDENT(...) do {} while (false)
#define LOG(...) do {} while (false)
#define DUMP(...) do {} while (false)
#endif

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

namespace {

struct Jelly {
  int mass;
  int price;

  Jelly(int m, int p):
    mass{m}, price{p}
  {
  }
};

vector<long long> dijkstra(vector<long long> const& deltas, int m)
{
  vector<long long> res(m, -1);
  vector<bool> done(m);
  priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> q;

  q.push({0, 0});
  res[0] = 0;

  while (!q.empty()) {
    auto [price, mass] = q.top();
    q.pop();
    if (done[mass]) continue;
    done[mass] = true;
    for (int i = 0; i < m; ++i) {
      if (deltas[i] < 0) continue;
      int new_mass = mass + i;
      if (new_mass >= m) new_mass -= m;
      if (res[new_mass] < 0 || res[new_mass] > price + deltas[i]) {
        res[new_mass] = price + deltas[i];
        q.push({res[new_mass], new_mass});
      }
    }
  }

  return res;
}

vector<long long> solve(vector<vector<Jelly>> const& jellies, int m)
{
  vector<long long> cur(m, -1);
  cur[0] = 0;
  vector<long long> next;
  for (auto const& color: jellies) {
    next.clear();
    next.resize(m, -1);
    for (auto const& jelly: color) {
      for (int mass = 0; mass < m; ++mass) {
        if (cur[mass] < 0) continue;
        int new_mass = mass + jelly.mass;
        if (new_mass >= m) new_mass -= m;
        if (next[new_mass] < 0 || next[new_mass] > cur[mass] + jelly.price) {
          next[new_mass] = cur[mass] + jelly.price;
        }
      }
    }
    swap(next, cur);
  }

  return dijkstra(cur, m);
}

}

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

  int n, k, m;
  cin >> n >> k >> m;

  vector<vector<Jelly>> jellies(k);
  for (int i = 0; i < n; ++i) {
    int color, mass, price;
    cin >> color >> mass >> price;
    jellies[color - 1].emplace_back(mass % m, price);
  }

  auto res = solve(jellies, m);
  for (auto const& x: res) {
    cout << x << '\n';
  }

  return 0;
}