/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 1ms 400.0 KiB
#3 Accepted 1ms 328.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 16ms 532.0 KiB
#6 Accepted 12ms 532.0 KiB
#7 Accepted 31ms 2.02 MiB
#8 Accepted 67ms 2.02 MiB
#9 Accepted 45ms 1.891 MiB
#10 Accepted 37ms 1.879 MiB
#11 Accepted 13ms 576.0 KiB
#12 Accepted 33ms 576.0 KiB
#13 Accepted 22ms 2.004 MiB
#14 Accepted 43ms 1.953 MiB
#15 Accepted 15ms 532.0 KiB
#16 Accepted 36ms 584.0 KiB
#17 Accepted 15ms 588.0 KiB

Code

/*
 * Name : Md. Fahmidur Rahman Nafi
 * Date : 2025-07-14   Time : 21:45:33
 */

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define ld long double
#define ull unsigned long long
#define lcm(a,b) ((a*b)/__gcd(a,b))
#define debug(x) cout << "Debug : " << x << endl;
const double PI = 2 * acos(0.0);
const int MOD = 1000000007;

void solve(){
    int x;
    cin >> x;
    vector <int> a(x); 
    deque <int> p, n;
    for (auto &i : a) cin >> i, i >= 0 ? p.push_back(i) : n.push_back(i);
    
    ll cnt = 0;    
    sort(p.begin(), p.end());
    sort(n.rbegin(), n.rend());
    ll ans = -1;
    for (int i = 1; i <= x; i++){
        if (!p.empty()){
            if (i % 2 != 0){    // kmns --> maximize
                cnt += p.back();
                p.pop_back();
            }
            else{
                cnt -= p.back();
                p.pop_back();
            }
        }
        else{
            if (i % 2 != 0){
                if (i != 1){
                    break;
                }
                else{
                    cnt += n.front();
                    n.pop_front();
                }
            }
            else{
                if (i != 2){
                    break;
                }
                else{
                    cnt += (-n.front());
                    n.pop_front();
                }
            }
        }

        // debug(cnt);
    }

    cout << cnt << endl;
}

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

    int t;
    cin >> t;

    while(t--){
        solve();
    }
}

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:00:10
Judged At
2025-07-14 17:00:10
Judged By
Score
100
Total Time
67ms
Peak Memory
2.02 MiB