17952022-12-03 18:19:39kovacs.peter.18fBenzinkút üzemeltetés (55)cpp11Accepted 55/554ms4152 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>

using namespace std;

struct point {
    int distance, benefit, max_benefit;
};

int main() {
    cin.sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;
    vector<point> dpS(N);    
    for (int i = 0; i < N; i++) {
        cin >> dpS[i].distance >> dpS[i].benefit;
        dpS[i].max_benefit = dpS[i].benefit;
        for (int j = 0; j < i; j++) {
            dpS[i].max_benefit = max(dpS[i].max_benefit, dpS[j].max_benefit + (dpS[j].distance + K <= dpS[i].distance ? dpS[i].benefit : 0));
        }
    }

    stack<int> answerS;
    int max_index = max_element(dpS.begin(), dpS.end(), [](point a, point b) { return a.max_benefit < b.max_benefit; }) - dpS.begin();
    answerS.push(max_index);
    while (dpS[0].distance + K <= dpS[answerS.top()].distance) {
        int current_max = 0;
        for (int i = 1; i < answerS.top() && dpS[i].distance + K <= dpS[answerS.top()].distance; i++) {
            if (dpS[i].max_benefit > dpS[current_max].max_benefit) {
                current_max = i;
            }
        }
        answerS.push(current_max);
    }
    cout << dpS[max_index].max_benefit << '\n' << answerS.size() << " ";
    while (!answerS.empty()) {
        cout << answerS.top() + 1 << " ";
        answerS.pop();
    }
    cout << '\n';
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/03ms1828 KiB
2Accepted0/04ms2052 KiB
3Accepted3/33ms2248 KiB
4Accepted3/33ms2360 KiB
5Accepted3/33ms2560 KiB
6Accepted3/32ms2640 KiB
7Accepted3/33ms2880 KiB
8Accepted3/33ms2860 KiB
9Accepted3/33ms2980 KiB
10Accepted3/33ms3344 KiB
11Accepted3/33ms3520 KiB
12Accepted3/33ms3644 KiB
13Accepted4/43ms3720 KiB
14Accepted4/43ms3944 KiB
15Accepted5/53ms3936 KiB
16Accepted6/64ms4068 KiB
17Accepted6/64ms4152 KiB