#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair <int, pair<int, long long>> p;
const long long inf = 7000000000001;
int n, m, k;
vector <p> zelki;
long long dp[7000][7000];
void wczyt(){
    cin>>n>>k>>m;
    for(int i=0; i<n; i++){
        int a, b;
        long long c;
        cin>>a>>b>>c;
        zelki.push_back({a-1, {b, c}}); //kolory od 0
    }
    sort(zelki.begin(), zelki.end());
    for(int i=0; i<k; i++){
        for(int w=0; w<m; w++) dp[i][w]=inf;
    }
    dp[k-1][0]=0;
}
int main(){
    ios_base::sync_with_stdio(0);
    wczyt();
    for(int it=0; it<m; it++){
        for(int i=0; i<n; i++){//biezemy i=ty zelek
            int kolor= zelki[i].f;
            int waga = zelki[i].s.f;
            long long cena = zelki[i].s.s;
            int pop = kolor-1;
            if(pop==-1) pop+=k;
            for(int w=0; w<m; w++){
                dp[kolor][(w+waga)%m] = min(dp[kolor][(w+waga)%m], dp[pop][w]+cena);
            }
        }
    }
    for(int i=0; i<m; i++){
        if(dp[k-1][i]==inf) cout<<"-1\n";
        else cout<<dp[k-1][i]<<"\n";
    }
}
        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  | #include <bits/stdc++.h> #define f first #define s second using namespace std; typedef pair <int, pair<int, long long>> p; const long long inf = 7000000000001; int n, m, k; vector <p> zelki; long long dp[7000][7000]; void wczyt(){ cin>>n>>k>>m; for(int i=0; i<n; i++){ int a, b; long long c; cin>>a>>b>>c; zelki.push_back({a-1, {b, c}}); //kolory od 0 } sort(zelki.begin(), zelki.end()); for(int i=0; i<k; i++){ for(int w=0; w<m; w++) dp[i][w]=inf; } dp[k-1][0]=0; } int main(){ ios_base::sync_with_stdio(0); wczyt(); for(int it=0; it<m; it++){ for(int i=0; i<n; i++){//biezemy i=ty zelek int kolor= zelki[i].f; int waga = zelki[i].s.f; long long cena = zelki[i].s.s; int pop = kolor-1; if(pop==-1) pop+=k; for(int w=0; w<m; w++){ dp[kolor][(w+waga)%m] = min(dp[kolor][(w+waga)%m], dp[pop][w]+cena); } } } for(int i=0; i<m; i++){ if(dp[k-1][i]==inf) cout<<"-1\n"; else cout<<dp[k-1][i]<<"\n"; } }  | 
            
        
                    English