/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 38ms 560.0 KiB
#6 Accepted 19ms 532.0 KiB
#7 Accepted 62ms 1.402 MiB
#8 Accepted 103ms 1.312 MiB
#9 Accepted 110ms 1.434 MiB
#10 Accepted 97ms 1.637 MiB
#11 Accepted 52ms 568.0 KiB
#12 Accepted 44ms 324.0 KiB
#13 Accepted 65ms 1.637 MiB
#14 Accepted 104ms 1.195 MiB
#15 Accepted 45ms 532.0 KiB
#16 Accepted 55ms 556.0 KiB
#17 Accepted 36ms 532.0 KiB

Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
  int t;
  cin >> t;
  while (t--){
    ll n, a, S = 0;
    cin >> n;
    vector<int> P, N;
    for (int i = 0; i < n; i++){
      cin >> a;
      if (a > 0) P.push_back(a);
      else N.push_back(a);
    }
    sort(P.begin(), P.end());
    sort(N.begin(), N.end());
    if (P.size()){
      S += P.back();
      P.pop_back();
    } else if (N.size()){
      S += N.back();
      N.pop_back();
    }
    if (P.size()){
      S -= P.back();
      P.pop_back();
    } else if (N.size()){
      S -= N.back();
      N.pop_back();
    }
    ll cur = S;
    if (P.size()){
      S += P.back();
      P.pop_back();
    }
    if (cur >= S){
      cout << cur << endl;
      continue;
    }
    cur = S;
    ll mn = S;
    int last = 0;
    while (true){
      int f = 1;
      last = 0;
      if (P.size()){
        S -= P.back();
        P.pop_back();
        f = 0;
        cur = S;
      } 
      if (f) break;
      f = 1;
      if (P.size()){
        S += P.back();
        P.pop_back();
        f = 0;
        last = 1;
        mn = min(mn, S);
      }
      if (f or P.empty()) break;
    }
    if (!last) mn = min(mn, cur);
    cout << mn << endl;
  }
}

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:13:21
Judged At
2025-07-14 17:13:21
Judged By
Score
100
Total Time
110ms
Peak Memory
1.637 MiB