202682026-01-05 18:21:13PappMatyasBenzinkút üzemeltetés (55)cpp17Accepted 55/552ms520 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Station
{
    long long d;
    long long v;
    int idx;
};

int main()
{
    int n;
    long long k;
    cin >> n >> k;

    vector<Station> s(n);

    for (int i = 0; i < n; i++)
    {
        cin >> s[i].d >> s[i].v;
        s[i].idx = i + 1;
    }

    vector<long long> dist(n);
    for (int i = 0; i < n; i++)
    {
        dist[i] = s[i].d;
    }

    vector<int> prev(n);
    for (int i = 0; i < n; i++)
    {
        long long target = s[i].d - k;
        int j = (int)(upper_bound(dist.begin(), dist.end(), target) - dist.begin()) - 1;
        prev[i] = j;
    }

    vector<long long> dp(n);
    dp[0] = s[0].v;

    for (int i = 1; i < n; i++)
    {
        long long take = s[i].v;
        if (prev[i] != -1)
        {
            take += dp[prev[i]];
        }
        long long skip = dp[i - 1];
        dp[i] = max(take, skip);
    }

    vector<int> chosen;
    int i = n - 1;

    while (i >= 0)
    {
        long long skip = (i == 0 ? 0 : dp[i - 1]);
        long long take = s[i].v + (prev[i] == -1 ? 0 : dp[prev[i]]);

        if (take >= skip)
        {
            chosen.push_back(s[i].idx);
            i = prev[i];
        }
        else
        {
            i--;
        }
    }

    reverse(chosen.begin(), chosen.end());

    cout << dp[n - 1] << "\n";
    cout << chosen.size();
    for (int x : chosen)
    {
        cout << " " << x;
    }
    cout << "\n";
}

SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/01ms316 KiB
2Accepted0/02ms316 KiB
3Accepted3/31ms520 KiB
4Accepted3/31ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms316 KiB
7Accepted3/31ms316 KiB
8Accepted3/31ms316 KiB
9Accepted3/31ms316 KiB
10Accepted3/31ms500 KiB
11Accepted3/31ms316 KiB
12Accepted3/31ms316 KiB
13Accepted4/42ms316 KiB
14Accepted4/41ms316 KiB
15Accepted5/51ms316 KiB
16Accepted6/61ms316 KiB
17Accepted6/61ms316 KiB