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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int mx = 7002;
const long long INFINITY = 9223372036854775807;
int n, k, m, color, weight;
long long price;

struct food{
    int weight;
    long long price;
};

vector<food> foods[mx];
vector<food>edges;
long long backpack[mx][mx];
long long dist[mx];
bool is_visited[mx];

priority_queue<pair<long long,int>> pq;

int main(){
    ios::sync_with_stdio(false);
    cin >> n >> k >> m;

    for(int i = 0; i < n; ++i){
        cin >> color >> weight >> price;
        foods[color].push_back({weight, price});
    }

    backpack[0][0] = 0;
    for(int i = 1; i < m; ++i){
        backpack[0][i] = INFINITY;
    }

    for(int i = 1; i <= k; ++i){
        if(foods[i].empty()){
            cout << 0 << "\n";
            for(int j = 1; j < m; ++j){
                cout << -1 << "\n";
            }
            return 0;
        }

        for(int j = 0; j < m; ++j){
            backpack[i][j] = INFINITY;
        }

        for(int j = 0; j < foods[i].size(); ++j){
            weight = foods[i][j].weight;
            price = foods[i][j].price;
            for(int t = 0; t < m; ++t){
                if(backpack[i-1][t] != INFINITY){
                    int new_weight = (t + weight) % m;
                    long long new_price = backpack[i-1][t] + price;
                    backpack[i][new_weight] = min(backpack[i][new_weight], new_price);
                }
            }
        }
    }

    for(int i = 1; i < m; ++i){
        if(backpack[k][i] != INFINITY){
            edges.push_back({i, backpack[k][i]});
        }
    }


    for(int i = 1; i < m; ++i){
        dist[i] = INFINITY;
    }

    pq.push({0, 0});

    while(!pq.empty()){
        auto actual = pq.top();
        pq.pop();

        price = -actual.first;
        weight = actual.second;

        is_visited[weight] = true;

        for(int i = 0; i < edges.size(); ++i){
            int new_weight = (weight + edges[i].weight) % m;
            if(!is_visited[new_weight]){
                long long new_price = price + edges[i].price;
                if(new_price < dist[new_weight]){
                    dist[new_weight] = new_price;
                    pq.push({-new_price, new_weight});
                }
            }
        }
    }

    for(int i = 0; i < m; ++i){
        if(dist[i] == INFINITY)
            cout << -1 << "\n";
        else
            cout << dist[i] << "\n";
    }

    return 0;
}