/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 392.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Wrong Answer 34ms 532.0 KiB
#6 Wrong Answer 22ms 532.0 KiB

Code

#include <bits/stdc++.h>
#define nl '\n'
#define ll long long
#define all(c) c.begin(),c.end()
#define print(c) for(auto e : c) cout << e << " "; cout << nl
using namespace std;
void solve()
{
    int n; cin >> n;
    vector<int> a(n);
    for(auto &e : a) cin >> e;
    sort(all(a)); reverse(all(a));

    multiset<int> neg, pos;
    for(auto e : a)
    {
        if(e >= 0) pos.insert(e);
        else neg.insert(e);
    }

    // cout << neg.size() << " " << pos.size() << nl; // ok

    ll curr_sum = 0;
    ll mx_at_any_point = -1E18, mn_at_any_point = 1E18;

    for (int i = 0; i < n; i++)
    {
        if(i%2 == 0) // roy's turn
        {
            curr_sum += a[i];

            if(a[i] >= 0)
            {
                auto it = pos.find(a[i]);
                pos.erase(it);
            }
            else 
            {
                auto it = neg.find(a[i]);
                neg.erase(it);
            }
        }
        else
        {
            if(!pos.empty())
            {
                auto it = pos.begin();
                curr_sum -= *it;
                --pos.begin();
            }
            else 
            {
                auto it = --neg.end();
                curr_sum -= *it;
                --neg.end();
            }
        }

        if((i & 1)) mn_at_any_point = min(mn_at_any_point, curr_sum);
        else mx_at_any_point = max(mx_at_any_point, curr_sum);
        
        if(i >= 1) // maybe i should not stop there for at least one person
        {
            // cout << mx_at_any_point << " <--> " << mn_at_any_point << nl;
            // vector<ll> k; k.push_back(mx_at_any_point), k.push_back(mn_at_any_point);
            // sort(all(k));
            // mn_at_any_point = k[0], mx_at_any_point = k[1];
            if(1)
            {
                // cout << "#" << i << nl;
                if((i & 1) && (pos.empty() && neg.size() == 1)); // Hriday knows Roy will add neg, it will minimize
                else if((i & 1) && (pos.empty() && neg.size() != 1))
                {
                    cout << mn_at_any_point << nl;  return;
                }

                // if((i % 2 == 0)); // Roy alternates the turn because there are only pos remaining to maximize
                // else if(i % 2 == 0)
                // {
                //     cout << mx_at_any_point << nl; return;
                // }

                if(i == n - 1)
                {
                    if(i & 1) cout << mn_at_any_point << nl; 
                    else cout << mx_at_any_point << nl; 
                    return;
                }
            }
        }
    }
    
    // cout << mx_at_any_point << nl;
    // cout << mx_at_any_point << " -- " << mn_at_any_point << nl;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int t; cin >> t;
    for(int tt = 1; tt <= t; tt++)
    {
        // cout << "TEST CASE-" << tt << nl;
        solve();
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1208 C. Game on Integer
Contest
Educational Round 1
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-14 17:24:52
Judged At
2025-07-14 17:24:52
Judged By
Score
5
Total Time
34ms
Peak Memory
532.0 KiB