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 <bits/stdc++.h>

using namespace std;
#define ll long long
#define maxn 7010
ll one_step[maxn], tmp[maxn], final[maxn];
vector<pair<int,int>> zelki[maxn];
bool odw[maxn];
int cnt = 0;

void set_infty(int m, ll *arr) {
    for (int i = 0; i < m; ++i){
        arr[i] = LLONG_MAX;
    }
}


void update_tmp(int m, pair<int,int> zelek) {
    for (int i = 0; i < m; ++i) {
        int prev = (i - zelek.first + m) % m;
        if (one_step[prev] != LLONG_MAX) {
            tmp[i] = min(tmp[i], one_step[prev] + (ll)zelek.second);
        }
    }
}

void calculate_one_step(int k, int m) {
    set_infty(m, one_step);
    one_step[0] = 0;
    for (int i = 1; i <=k; ++i) {
        if ((int)zelki[i].size() == 0) {
            set_infty(m, one_step);
            break;
        }
        set_infty(m, tmp);
        for (int j = 0; j < (int)zelki[i].size(); ++j) {
            update_tmp(m, zelki[i][j]);
        }
        swap(one_step, tmp);
    }
}

void update_final(int m, int el, ll val) {
    if (val == LLONG_MAX) {
        return;
    }
    for (int i = 1; i < m; ++i) {
        if (one_step[i] != LLONG_MAX) {
            int next = (i + el) % m;
            final[next] = min(final[next], one_step[i] + val);
        }
    }
}

int find_min(int m) {
    int res = -1;
    for (int i = 0; i < m; ++i) {
        if (!odw[i] && (res < 0 || final[res] > final[i])){
            res = i;
        }
    }
    return max(res, 0);
}


//void output(int m);

void calculate_final(int m) {
    final[0] = 0;
    for (int i = 1; i < m; ++i) {
        final[i] = one_step[i];
    }
    //output(m);
    //exit(0);
    for (int i = 0; i < m; ++i) {
        int x = find_min(m);
        odw[x] = true;
        update_final(m, x, final[x]);
    }
}

void output(int m) {
    for (int i = 0; i < m; ++i) {
        if (final[i] == LLONG_MAX) {
            cout << -1 << endl;
        }
        else {
            cout << final[i] << endl;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,k,m,a,b,c;
    cin >> n >> k >> m;
    for (int i = 0; i < n; ++i) {
        cin >> a >> b >> c;
        zelki[a].push_back({b % m,c});
    }
    calculate_one_step(k, m);
    calculate_final(m);
    output(m);   
}