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
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
#include <limits>
#include <functional>

const int LOG = 13;
const long long INF = std::numeric_limits<long long>::max() / 2;

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

    int N, K, M;
    std::cin >> N >> K >> M;

    std::vector<std::vector<std::pair<int, long long>>> colors(K);

    auto add = [&](int a, int b) {
        return a + b < M ? a + b : a + b - M;
    };

    for(int i = 0; i < N; i++) {
        int k, m;
        long long c;
        std::cin >> k >> m >> c;
        colors[k - 1].push_back({m % M, c}); 
    }

    std::vector<long long> min_cost(M, INF);
    min_cost[0] = 0;
    for(int k = 0; k < K; k++) {
        std::vector<long long> dp(M, INF);
        for(auto [m, c] : colors[k]) {
            for(int i = 0; i < M; i++) {
                dp[add(i, m)] = std::min(dp[add(i, m)], min_cost[i] + c);
            }
        }
        min_cost = dp;
    }

    std::vector<long long> dp = min_cost;
    dp[0] = 0;
    for(int k = 0; k < M; k++) {
        long long w = min_cost[k], r = k;
        for(int p = 0; p < LOG; p++) {
            for(int i = 0; i < M; i++) {
                dp[add(i, r)] = std::min(dp[add(i, r)], dp[i] + w);
            }
            w *= 2;
            r = add(r, r);
            if(w >= INF) {
                break;
            }
        }
    }

    for(int i = 0; i < M; i++) {
        std::cout << (dp[i] == INF ? -1 : dp[i]) << '\n';
    }

    return 0;
}