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
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <unordered_map>
#include <string>
#include <queue>
#include <deque>
#include <set>
#include <cstdlib>
#include <ctime>
#include<chrono>
#include<thread>
#include<iomanip>
#include<fstream>
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

using namespace std;
int ultradp[7001][7001];

int main()
{
    int n, k, m;
    cin >> n >> k >> m;
    map<int, int> kolory[k + 1];
    int ki, mi, ci;
    for(int i = 0; i < n; i++) {
        cin >> ki >> mi >> ci;
        if(kolory[ki].count(mi) && kolory[ki][mi] > ci) kolory[ki][mi] = ci;
        else if(!kolory[ki].count(mi)) kolory[ki][mi] = ci;
    }
    for(auto i : kolory[1]) {
        ultradp[1][i.first % m] = i.second;
    }
    for(int i = 2; i < k + 1; i++) {
        for(int j = 0; j < m; j++) {
            if(ultradp[i - 1][j] != 0) {
                for(auto it : kolory[i]) {
                    int cela = (j + it.first) % m;
                    if(ultradp[i][cela] == 0) ultradp[i][cela] = ultradp[i - 1][j] + it.second;
                    else {
                        ultradp[i][cela] = min(ultradp[i][cela], ultradp[i - 1][j] + it.second);
                    }
                }
            }
        }
    }
//    for(int i = 0; i < m; i++) {
//        cout << ultradp[k][i] << endl;
//    }
    int dp[m];
    for(int i = 0; i < m; i++) {
        if(i == 0) dp[i] = 0;
        else if(ultradp[k][i] == 0) dp[i] = -1;
        else dp[i] = ultradp[k][i];
    }
    bool nothing = false;
    while(nothing != true) {
        nothing = true;
        for(int i = 1; i < m; i++) {
            if(dp[i] != -1) {
                for(int j = m - 1; j >= 1; j--) {
                    if(dp[j] != -1) {
                        int cel = (i + j) % m;
                        if(dp[cel] == -1) {
                            dp[cel] = dp[i] + dp[j];
                            nothing = false;
                        }
                        else if (dp[cel] > dp[i] + dp[j]) {
                            dp[cel] = dp[i] + dp[j];
                            nothing = false;
                        }
                    }
                }
            }
        }
    }
        for(int i = 0; i < m; i++) {
            cout << dp[i] << endl;
        }
    return 0;
}