/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 324.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 38ms 532.0 KiB
#6 Accepted 23ms 532.0 KiB
#7 Accepted 42ms 2.023 MiB
#8 Accepted 103ms 2.02 MiB
#9 Accepted 104ms 1.27 MiB
#10 Accepted 99ms 2.055 MiB
#11 Accepted 51ms 552.0 KiB
#12 Accepted 43ms 532.0 KiB
#13 Accepted 67ms 2.047 MiB
#14 Accepted 101ms 2.039 MiB
#15 Accepted 41ms 532.0 KiB
#16 Accepted 54ms 532.0 KiB
#17 Accepted 36ms 532.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
 
 
const int MOD = 1e9 + 7;
const int MX = 1e6 + 10;
long long dp[MX + 100][2];
 
 
void solve(){
  int n;
  cin >> n;
  vector<int> a(n);
  for(int &i : a) cin >> i;
  sort(a.begin(),a.end());
  if(a[n-2] < 0){
    cout << a[n-1] - a[n-2] << endl;
    return;
  }
  else{
    vector<int> dp(n+1);
    for(int i = 1; i <= n ; i++){
      int t = n - i;
      if(t % 2 == 0){
        dp[i] = max(dp[i],dp[i-1] + a[i-1]);
      }
      else{
        dp[i] = min(dp[i],dp[i-1] - a[i-1]);
      }
    }
    cout << dp[n] << endl;
  }
  
  
  
}
 
 
 
 
int main()
{
    int t = 1;
    cin >> t;
    
    while(t--) 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:17:18
Judged At
2025-07-14 17:17:18
Judged By
Score
100
Total Time
104ms
Peak Memory
2.055 MiB