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

long long dyn[7010][7010], n, k, m, res[7010];
bool visited[7010];

int main() {
    std::vector<std::pair<int, std::pair<int, long long>>> input;

    for (int i = 0; i < 7010; i++) {
        res[i] = -1;
        for (int j = 0; j < 7010; j++) {
            dyn[i][j] = -1;
        }
    }
    res[0] = 0;
    dyn[0][0] = 0;

    std::cin >> n >> k >> m;

    for (int i = 0; i < n; i++) {
        long long kolor, masa, cena;
        std::cin >> kolor >> masa >> cena;
        input.emplace_back(kolor, std::pair(masa, cena));
    }

    std::sort(input.begin(), input.end());

    for (int i = 0; i < n; i++) {
        long long kolor = input[i].first;
        long long masa = input[i].second.first;
        long long cena = input[i].second.second;
        for (int j = 0; j < m; j++) {
            if (dyn[kolor - 1][j] >= 0 && (dyn[kolor][(j + masa) % m] == -1 || dyn[kolor][(j + masa) % m] > dyn[kolor - 1][j] + cena))
                dyn[kolor][(j + masa) % m] = dyn[kolor - 1][j] + cena;
        }
    }

    for (int i = 1; i < m; i++) {
        if (dyn[k][i] < 0) continue;

        for (int j = 0; j < m; j++) {
            visited[j] = false;
        }

        for (int j = 0; j < m; j++) {
            long long cur = j;
            if (!visited[cur] && res[cur] >=0 ) {
                while (!visited[cur]) {
                    visited[cur] = true;
                    if (res[(cur + i) % m] == -1 || res[(cur + i) % m] > res[cur] + dyn[k][i]) {
                        res[(cur + i) % m] = res[cur] + dyn[k][i];
                    }
                    cur = (cur + i) % m;
                }
            }
        }
    }

    for (int i = 0; i < m; i++) {
        std::cout << res[i] << "\n";
    }

    return 0;
}