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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <bits/stdc++.h>

using namespace std;

long long amount;
long long amountOfColors;
long long m;

vector<long long> groups[7007];

long long weights[7007];
long long prices[7007];

long long dp[7007][7007];

long long colors[7007];

long long answers[7007];

vector<long long> order;

void printDp()
{
    for (int i = 1; i <= amountOfColors; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            cout << dp[i][j] << ' ';
        }
        cout << '\n';
    }
    cout << '\n';
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> amount >> amountOfColors >> m;

    for (int i = 1; i <= amount; ++i)
    {
        int color, weight, price;
        cin >> color >> weight >> price;
        groups[color].push_back(i);
        colors[i] = color;
        weights[i] = weight;
        prices[i] = price;
    }

    int minGroupSize = 10000000;
    int maxGroupSize = 0;
    for (int i = 1; i <= amountOfColors; ++i)
    {
        maxGroupSize = max(maxGroupSize, (int) groups[i].size());
        minGroupSize = min(minGroupSize, (int) groups[i].size());
    }
    if (minGroupSize == 0)
    {
        cout << "0\n";
        for (int i = 1; i < m; ++i) cout << "-1\n";
        return 0;
    }

    if (amountOfColors == amount)
    {
        long long weightsSum = 0, priceSum = 0;
        for (int i = 1; i <= amount; ++i)
        {
            weightsSum += weights[i];
            priceSum += prices[i];
        }
        for (int i = 1; i < m; ++i)
        {
            long long moduloValue = (weightsSum * i) % m;
            long long totalPrice = priceSum * i;
            if (answers[moduloValue] == 0 || answers[moduloValue] > totalPrice) answers[moduloValue] = totalPrice;
        }
        cout << "0\n";
        for (int i = 1; i < m; ++i)
        {
            if (answers[i] != 0)
                cout << answers[i] << '\n';
            else
                cout << "-1\n";
        }
        return 0;
    }

    for (int i = 1; i <= amountOfColors; ++i)
    {
        for (int e : groups[i]) order.push_back(e);
    }

    for (int a = 1; a <= m; ++a)
    {
        for (long long i : order)
        {
            for (int j = 1; j <= m; ++j)
            {
                long long reference = j - weights[i];
                if (reference < 0) reference += m;
                if (reference == 0) reference = m;
                long long prevoius = colors[i] - 1;
                if (prevoius == 0) prevoius = amountOfColors;
                if (prevoius == amountOfColors && reference == m)
                {
                    long long value = a * prices[i];
                    if (dp[colors[i]][j] == 0)
                        dp[colors[i]][j] = value;
                    else
                        dp[colors[i]][j] = min(dp[colors[i]][j], value);
                }
                if (dp[prevoius][reference] != 0)
                {
                    long long value = dp[prevoius][reference] + prices[i];
                    if (dp[colors[i]][j] == 0)
                        dp[colors[i]][j] = value;
                    else
                        dp[colors[i]][j] = min(dp[colors[i]][j], value);
                }
            }
        }
    }

    for (int i = 0; i < m; ++i)
    {
        if (dp[amountOfColors][i] != 0 || i == 0)
            cout << dp[amountOfColors][i] << '\n';
        else
            cout << "-1\n";
    }
}