69722023-12-23 08:18:40GervidBenzinkút üzemeltetés (55)cpp17Accepted 55/554ms3872 KiB
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

int main()
{
    int n, k, i, j;
    cin >> n >> k;

    vector<pair<int, int>> places(n);

    for (i = 0; i < n; i++)
    {
        cin >> places[i].first >> places[i].second; //dist, profit
    }

    vector<int> opt(n); // profit
    vector<int> last(n, -1);

    for (i = 0; i < n; i++)
    {
        int max = 0, maxi = -1;
        for (j = 0; j < i; j++)
        {
            if (max < opt[j] && places[j].first + k <= places[i].first)
            {
                max = opt[j];
                maxi = j;
            }
        }

        last[i] = maxi;
        opt[i] = max + places[i].second;
    }

    int max = 0, maxi = 0;
    for (j = 0; j < i; j++)
    {
        if (max < opt[j])
        {
            max = opt[j];
            maxi = j;
        }
    }

    cout << max << endl;

    int node = maxi;
    stack<int> reverse;

    while (node != -1)
    {
        reverse.push(node);
        node = last[node];
    }

    cout << reverse.size();

    while (reverse.size() > 0)
    {
        cout << ' ' << reverse.top() + 1;
        reverse.pop();
    }
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/03ms1688 KiB
2Accepted0/04ms1868 KiB
3Accepted3/33ms2064 KiB
4Accepted3/33ms2276 KiB
5Accepted3/33ms2356 KiB
6Accepted3/33ms2600 KiB
7Accepted3/33ms2716 KiB
8Accepted3/33ms2828 KiB
9Accepted3/33ms2976 KiB
10Accepted3/33ms3224 KiB
11Accepted3/33ms3364 KiB
12Accepted3/33ms3460 KiB
13Accepted4/43ms3664 KiB
14Accepted4/43ms3748 KiB
15Accepted5/53ms3748 KiB
16Accepted6/64ms3640 KiB
17Accepted6/64ms3872 KiB