結果

問題 No.2812 Plus Minus Blackboard
ユーザー t98slider
提出日時 2024-07-20 13:16:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,226 bytes
コンパイル時間 2,029 ms
コンパイル使用メモリ 199,192 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-20 02:15:42
合計ジャッジ時間 3,772 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    vector<int> A(N);
    cin >> A;
    sort(A.begin(), A.end());

    ll s = accumulate(A.begin(), A.end(), 0ll);
    int l = 0, r = N - 1;
    auto f = [&](int p){
        if(l != 0 || r != N - 1){
            if(A[l] <= 0 || A[r] <= 0){
                cout << "Yes\n";
                exit(0);
            }
            if(A[l] >= 0 || A[r] >= 0){
                cout << "Yes\n";
                exit(0);
            }
        }
        if(A[p] >= 0 && s - A[p] <= 0) return;
        if(A[p] <= 0 && s - A[p] >= 0) return;
        cout << "No\n";
        exit(0);
    };
    while(l < r){
        if(s == 0){
            if(abs(A[l]) >= abs(A[r])){
                f(l);
                s -= A[l++];
            }else{
                f(r);
                s -= A[r--];
            }
        }else if(s > 0){
            f(r);
            s -= A[r--];
        }else{
            f(l);
            s += A[l++];
        }
    }
    cout << "Yes\n";
}
0