結果

問題 No.3268 As Seen in Toasters
ユーザー D M
提出日時 2025-09-19 16:33:15
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,555 bytes
コンパイル時間 3,242 ms
コンパイル使用メモリ 274,736 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-19 16:33:21
合計ジャッジ時間 5,668 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; 
    if (!(cin >> N)) return 0;

    const long long INF = (1LL<<62);
    long long Pmin = INF, Pmax = -INF; // 非負の最小・最大
    long long S = 0;                   // 負の絶対値合計
    long long a1 = 0, a2 = 0;          // 負の絶対値の最大・次点
    int p = 0, m = 0;                  // 非負/負 の個数

    for (int i = 0; i < N; ++i) {
        long long x; cin >> x;
        if (x >= 0) {
            ++p;
            Pmin = min(Pmin, x);
            Pmax = max(Pmax, x);
        } else {
            ++m;
            long long a = -x;
            S += a;
            if (a >= a1) { a2 = a1; a1 = a; }
            else if (a > a2) { a2 = a; }
        }
    }

    long long S_edges = 0;
    if (m == 0) {
        // 非負のみ
        S_edges = (p >= 2 ? Pmax - Pmin : 0);
    } else if (p == 0) {
        // 負のみ(全枝が根から伸びる)
        S_edges = S;
    } else {
        // 負も非負もある:根から負枝合計 S と正枝の端 Pmax までを含む
        S_edges = S + Pmax;
    }

    long long D = 0; // 直径
    if (m >= 2) D = max(D, a1 + a2);     // 負最大2本の先端間
    if (p >= 1) D = max(D, a1 + Pmax);   // 負最大と正の最大
    if (p >= 2) D = max(D, Pmax - Pmin); // 正の区間の端同士(※ここは差です)

    long long ans = 2 * S_edges - D;
    cout << ans << '\n';
    return 0;
}
0