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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ssize(a) ((int)(a.size()))
#define inf (LONG_LONG_MAX / 2)
#define modadd(a, b, m) (a + b < m? a + b: a + b - m)

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

    int n, k, m;
    cin >> n >> k >> m;
    vector<vector<pair<int, ll>>>zel(k + 1);
    for(int i = 0; i < n; ++i)
    {
        int ks, ms;
        ll c;
        cin >> ks >> ms >> c;
        zel[ks].push_back({ms, c});
    }
    vector<vector<ll>>dp(2, vector<ll>(m, inf));
    dp[0][0] = 0;
    for(int i = 1; i <= k; ++i)
    {
        for(int j = 0; j < m; ++j)
            dp[i & 1][j] = inf;
        for(int j = 0; j < m; ++j)
            for(pair<int, ll> l: zel[i])
                    dp[i & 1][modadd(j,  l.first, m)] = min(dp[i & 1][modadd(j,  l.first, m)], dp[!(i & 1)][j] + l.second);
    }
    vector<ll>pojzel(m);
    for(int i = 0; i < m; ++i)
    {
        dp[0][i] = dp[k & 1][i];
        dp[1][i] = inf;
    }
    dp[1][0] = 0;
    for(int i = 0; i < m; ++i)
    {
        if(dp[0][i] == inf)
            continue;
        int ti = i, j = 1;
        while(ti != 0)
        {
            dp[1][ti] = min(dp[1][ti], dp[0][i] * j);
            ti = (ti + i) % m;
            ++j;
        }
    }
    for(int i = 1; i < m; ++i)
        dp[0][i] = inf;
    dp[0][0] = 0;
    for(int i = 0; i < m; ++i)
    {
        if(dp[1][i] == inf)
            continue;
        for(int j = 0; j < m; ++j)
            dp[0][modadd(j, i, m)] = min(dp[0][modadd(j, i, m)], dp[0][j] + dp[1][i]);
    }
    for(auto i: dp[0])
        if(i == inf)
            cout << -1 << endl;
        else
            cout << i << endl;
    return 0;
}