結果

問題 No.3185 Three Abs
ユーザー jiangxinyang
提出日時 2025-06-20 21:44:25
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 1,119 bytes
コンパイル時間 1,875 ms
コンパイル使用メモリ 198,820 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-20 21:44:32
合計ジャッジ時間 6,470 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
const int N = 200005;
const ll INF = 0x3f3f3f3f3f3f3f3f;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int _;
    cin >> _;
    while (_--) {
        int n;
        cin >> n;
        vector<ll> a(n), sum(n);
        for (int i = 0; i < n; i++) cin >> a[i];
        sum[0] = a[0];
        for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + a[i];
        ll tot = sum[n - 1];
        ll ans = 0;
        ll X = -INF, Y = -INF;
        if (sum[0] >= 0)
            X = 2 * sum[0];
        else
            Y = -2 * sum[0];
        for (int j = 1; j < n - 1; j++) {
            ll g = max(sum[j], -sum[j]);
            if (X != -INF) g = max(g, X - sum[j]);
            if (Y != -INF) g = max(g, sum[j] + Y);
            ll c1 = tot - sum[j];
            ll c2 = g + llabs(c1);
            ans = max(ans, c2);
            if (sum[j] >= 0)
                X = max(X, 2 * sum[j]);
            else
                Y = max(Y, -2 * sum[j]);
        }
        cout << ans << "\n";
    }
    return 0;
}
0