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
134
135
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    long long n, k, m;
    std::cin >> n >> k >> m;

    std::map<long long, std::map<long long, long long>> colorsPrices;

    for (int i = 0; i < n; i++)
    {
        long long ki, mi, ci;

        std::cin >> ki >> mi >> ci;

        long long r = mi % m;

        auto it = colorsPrices[ki].find(r);
        if (it != colorsPrices[ki].end())
        {
            it->second = std::min(it->second, ci);
        }
        else
        {
            colorsPrices[ki][r] = ci;
        }
    }

    if (colorsPrices.size() < k)
    {
        std::cout << 0 << std::endl;
        for (int i = 1; i < m; i++)
        {
            std::cout << -1 << std::endl;
        }

        return 0;
    }

    std::map<long long, long long> singlePrices;

    singlePrices[0] = 0;

    for (auto &[k, v] : colorsPrices)
    {
        std::map<long long, long long> withNewColorPrices;

        for (auto &[r, c] : singlePrices)
        {
            for (auto &[r2, c2] : v)
            {
                long long new_r = r + r2;
                if (new_r >= m)
                {
                    new_r %= m;
                }

                auto it = withNewColorPrices.find(new_r);

                if (it != withNewColorPrices.end())
                {
                    it->second = std::min(it->second, c + c2);
                }
                else
                {
                    withNewColorPrices.emplace(new_r, c + c2);
                }
            }
        }

        singlePrices = std::move(withNewColorPrices);
    }

    std::vector<long long> prices(m, 1e18);

    prices[0] = 0;

    std::priority_queue<std::pair<long long, long long>, std::vector<std::pair<long long, long long>>, std::greater<>> pricesToResolve;

    pricesToResolve.emplace(0, 0);

    while (!pricesToResolve.empty())
    {
        auto [c, r] = pricesToResolve.top();
        pricesToResolve.pop();

        if (prices[r] < c)
        {
            continue;
        }

        for (auto &[r2, c2] : singlePrices)
        {
            long long new_r = r + r2;
            if (new_r >= m)
            {
                new_r %= m;
            }

            long long new_c = c + c2;

            if (prices[new_r] > new_c)
            {
                prices[new_r] = new_c;
                pricesToResolve.emplace(c + c2, new_r);
            }
        }
    }

    for (int i = 0; i < m; i++)
    {
        if (prices[i] == 1e18)
        {
            prices[i] = -1;
        }
    }

    for (int i = 0; i < m; i++)
    {
        std::cout << prices[i] << std::endl;
    }

    return 0;
}