172582025-06-08 12:07:11algoproGamecpp17Time limit exceeded 30/1002.599s5432 KiB
// UUID: dda06e14-2abe-4ed6-a9e8-450076f1f4ca
#include <iostream>
#include <vector>
#include <set> // Include the header for multiset

const int MAX_N = 1e5;
int a[MAX_N];

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

    int n, k;
    std::cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
    }
        
    for (; k > 0; --k) {
        int p;
        std::cin >> p;

        // Use std::multiset to correctly handle duplicate cards while keeping them sorted.
        std::multiset<int> available_cards;
        for (int i = 0; i < p; ++i) {
            available_cards.insert(a[i]);
        }
            
        int pocket = 0;
        long long score = 0;
        int player = 1;
        
        // This variable correctly mimics the stateful 'mx' from the original logic.
        // It's the boundary used to decide where the opponent's next card goes.
        int mx_boundary = n; 

        // --- Main game simulation loop 1 ---
        for (int i = p; i < n; ++i, player *= -1) {
            int card_to_play;
            if (pocket) {
                card_to_play = pocket;
                score += (long long)card_to_play * player;
                pocket = 0;
                // Note: mx_boundary is NOT updated when playing from the pocket.
            } else {
                // Get the largest element from the hand.
                auto it = prev(available_cards.end());
                card_to_play = *it;
                available_cards.erase(it);
                
                score += (long long)card_to_play * player;
                
                // CRUCIAL: Update the boundary ONLY when a card is played from the hand.
                mx_boundary = card_to_play;
            }

            // The opponent draws card a[i]. The comparison uses the persistent boundary.
            if (a[i] <= mx_boundary) {
                available_cards.insert(a[i]);
            } else {
                pocket = a[i];
            }
        }
        
        // --- Final turns simulation loop 2 ---
        // This loop now correctly simulates the end-game using the remaining cards.
        for (int i = 0; i < p; ++i, player *= -1) {
            if (pocket) {
                score += (long long)pocket * player;
                pocket = 0;
            } else {
                if (!available_cards.empty()) {
                    auto it = prev(available_cards.end());
                    int mx = *it;
                    available_cards.erase(it);
                    score += (long long)mx * player;
                }
            }
        }
        std::cout << score << '\n';
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask110/10
1Accepted1ms316 KiB
2Accepted1ms316 KiB
subtask220/20
1Accepted3ms316 KiB
2Accepted2ms316 KiB
3Accepted14ms316 KiB
4Accepted35ms316 KiB
subtask30/70
1Accepted340ms564 KiB
2Accepted365ms576 KiB
3Accepted1.245s824 KiB
4Accepted1.526s940 KiB
5Time limit exceeded2.585s3124 KiB
6Time limit exceeded2.599s2868 KiB
7Time limit exceeded2.592s2868 KiB
8Time limit exceeded2.584s4404 KiB
9Time limit exceeded2.582s4916 KiB
10Time limit exceeded2.584s5168 KiB
11Time limit exceeded2.579s5432 KiB
12Time limit exceeded2.581s4660 KiB
13Time limit exceeded2.582s5220 KiB
14Time limit exceeded2.599s5428 KiB