/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 760.0 KiB
#2 Accepted 1ms 324.0 KiB
#3 Accepted 1ms 324.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 15ms 740.0 KiB
#6 Accepted 11ms 764.0 KiB
#7 Accepted 31ms 9.625 MiB
#8 Accepted 47ms 9.582 MiB
#9 Accepted 47ms 9.68 MiB
#10 Accepted 44ms 9.609 MiB
#11 Accepted 12ms 596.0 KiB
#12 Accepted 12ms 568.0 KiB
#13 Accepted 28ms 9.574 MiB
#14 Accepted 46ms 9.598 MiB
#15 Accepted 13ms 576.0 KiB
#16 Accepted 15ms 812.0 KiB
#17 Accepted 14ms 532.0 KiB

Code

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

const int MAX = int(2e5);
const int RND = 853846273;

long long A[MAX + 5], dp[MAX + 5];

long long solve(int turn, int n)
{
    if (turn == n)
        return 0;
    
    int pos = turn;
    
    if (dp[pos] != RND)
        return dp[pos];
    
    long long res = 0;
    
    if (turn % 2 == 0)
        res = max(0LL, A[pos] + solve(turn + 1, n));
    else
        res = min(0LL, solve(turn + 1, n) - A[pos]);
    
    return dp[pos] = res;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;

    while (t--)
    {
        int n;
        cin >> n;
        
        for (int i = 0; i < n; i++)
        {
            cin >> A[i];
            dp[i] = RND;
        }
        
        sort (A, A + n, greater<>());
        
        long long res = A[0] - A[1] + solve(2, n);
        
        cout << res << "\n";
    }

    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 16:23:55
Judged At
2025-07-14 16:23:55
Judged By
Score
100
Total Time
47ms
Peak Memory
9.68 MiB