/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 484.0 KiB
#4 Accepted 1ms 536.0 KiB
#5 Accepted 14ms 344.0 KiB
#6 Accepted 10ms 532.0 KiB
#7 Accepted 22ms 2.062 MiB
#8 Accepted 39ms 1.996 MiB
#9 Accepted 37ms 2.246 MiB
#10 Accepted 64ms 2.023 MiB
#11 Accepted 11ms 536.0 KiB
#12 Accepted 13ms 580.0 KiB
#13 Accepted 45ms 2.062 MiB
#14 Accepted 39ms 2.02 MiB
#15 Accepted 13ms 532.0 KiB
#16 Accepted 33ms 604.0 KiB
#17 Accepted 37ms 576.0 KiB

Code

#pragma GCC optimize("O3", "inline")
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
#define pb push_back
#define ub upper_bound
#define INF 1e18 + 100

ll solve()
{
    int n;
    cin >> n;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    ll ans = 0;
    sort(a.begin(), a.end());
    int cnt = 0;
    for(int i = n - 1; i >= 0; i--){
        if(a[i] <= 0){
            if(cnt >= 2) return ans;
            if(cnt == 0) return a[n - 1] - a[n - 2];
            return ans + ((n - i - 1) % 2 == 0 ? a[i] : -a[i]);
        } 
        cnt++;
        ans += ((n - i - 1) % 2 == 0 ? a[i] : -a[i]);
    }
    return ans;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--)
    {
        ll x = solve();
        cout << x << "\n";
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1208 C. Game on Integer
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-15 05:16:44
Judged At
2025-07-15 05:16:44
Judged By
Score
100
Total Time
64ms
Peak Memory
2.246 MiB