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
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;

long long MAX = 2'000'000'000'000'000'000;

struct zelek {
    int weight;
    long long price;
};

bool operator<(const zelek &a, const zelek &b) {
    if (a.price > b.price) return true;
    if (a.price < b.price) return false;
    if (a.weight > b.weight) return true;
    return false;
}

vector<zelek> compute_steps(vector<long long> &source, vector<vector<zelek>> &zelki, int m) {
    vector<long long> target(source.size());
    for (vector<zelek> &color : zelki) {
        target.assign(target.size(), MAX);
        for (int p = 0; p < m; p++) {
            for (zelek &z : color) {
                int weight = (p + z.weight) % m;
                long long price = source[p] + z.price;
                if (target[weight] > price) {
                    target[weight] = price;
                }
            }
        }
        source.assign(target.begin(), target.end());
    }
    vector<zelek> res;
    for (int i = 0; i < source.size(); i++) {
        if (source[i] != MAX) {
            res.push_back({i, source[i]});
        }
    }

    return res;
}

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

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

    vector<vector<zelek>> zelki(k);
    for (int i = 0, k; i < n; i++) {
        cin >> k; k--;
        zelek z;
        cin >> z.weight >> z.price;
        zelki[k].push_back(z);
    }
    
    // if at least one color is lacking 
    for (int i = 0; i < k; i++) {
        if (zelki[i].empty()) {
            cout << 0 << "\n";
            for (int i = 1; i < m; i++) {
                cout << -1 << "\n";
            }
            
            return 0;
        }
    }

    vector<long long> source(m, MAX);
    source[0] = 0;

    vector<zelek> steps = compute_steps(source, zelki, m);
    priority_queue<zelek> pq(steps.begin(), steps.end());
    
    vector<long long> res(m, MAX);
    res[0] = 0;

    vector<long long> mns(m, MAX);
    for (zelek z: steps) {
        mns[z.weight] = z.price;
    }
    mns[0] = 0;

    while (! pq.empty()) {
        zelek z = pq.top(); pq.pop();
        if (res[z.weight] > z.price) {
            res[z.weight] = z.price;
            mns[z.weight] = z.price;
            for (auto &e : steps) {
                int weight = (z.weight + e.weight ) % m;
                long long price = z.price + e.price;
                if (mns[weight] > price) {
                    mns[weight] = price;
                    pq.push({weight, price});
                }
            }            
        }
    }
    
    for (long long &e: res) {
        if (e == MAX) cout << -1 << "\n";
        else cout << e << "\n";
    } 

    return 0;
}