/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 448.0 KiB
#4 Accepted 1ms 484.0 KiB
#5 Accepted 16ms 536.0 KiB
#6 Accepted 12ms 532.0 KiB
#7 Accepted 37ms 2.02 MiB
#8 Accepted 44ms 2.02 MiB
#9 Accepted 36ms 2.02 MiB
#10 Accepted 58ms 2.02 MiB
#11 Accepted 11ms 532.0 KiB
#12 Accepted 11ms 588.0 KiB
#13 Accepted 21ms 2.02 MiB
#14 Accepted 41ms 2.02 MiB
#15 Accepted 13ms 584.0 KiB
#16 Accepted 15ms 532.0 KiB
#17 Accepted 13ms 576.0 KiB

Code

#if __has_include("../stdc++.h")
	#include "../stdc++.h"
#else
	#include <bits/stdc++.h>
#endif

template <typename T>
std::istream &operator>>(std::istream &in, std::vector<T> &v)
{
    for (T &x : v)
        in >> x;
    return in;
}

template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v)
{
    for (std::size_t i = 0; i < v.size(); ++i)
        out << v[i] << (i + 1 == v.size() ? "" : " ");
    return out;
}

inline void yes() { std::cout << "Yes\n"; }
inline void no()  { std::cout << "No\n";  }

using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<long long> a(n);
    cin >> a;
    sort(a.begin(), a.end(), greater<long long>());
    long long curr = 0;
    curr += a[0];
    curr -= a[1];
    for (int i = 2; i < n; i++) {
        if (a[i] <= 0) break;
        if (i % 2 == 0) {
            if (a[i] > 0) {
                curr += a[i];
            }
        } else {
            if (curr - a[i] < curr) {
                curr -= a[i];
            }
            else {
                break;
            }
        }
    }
    cout << max(curr, a[0] - a[1]) << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1208 C. Game on Integer
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-14 18:09:57
Judged At
2025-07-14 18:09:57
Judged By
Score
100
Total Time
58ms
Peak Memory
2.02 MiB