結果

問題 No.3697 実力を揃える
コンテスト
ユーザー GOTKAKO
提出日時 2026-09-10 17:00:46
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 161 ms / 5,000 ms
+ 456µs
コード長 1,417 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,362 ms
コンパイル使用メモリ 222,776 KB
実行使用メモリ 31,496 KB
最終ジャッジ日時 2026-09-10 17:00:50
合計ジャッジ時間 3,832 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

    int N; cin >> N;
    vector<long long> X,Y;
    long long sum = 0;
    for(int i=0; i<N; i++){
        int a; cin >> a; sum += a;
        if(i%2 == 0) X.push_back(a);
        else Y.push_back(a);
    }
    int n = X.size(),m = Y.size(),n2 = 1<<n,m2 = 1<<m;
    vector<vector<long long>> Sx(n+1),Sy(m+1);
    Sx.at(0).push_back(0),Sy.at(0).push_back(0);
    vector<long long> S(n2);
    for(int i=0; i<n2; i++) for(int k=0; k<n; k++) if(i&(1<<k)){
        S.at(i) = S.at(i-(1<<k))+X.at(k);
        Sx.at(__popcount((unsigned)i)).push_back(S.at(i)); break;
    }
    S.resize(m2);
    for(int i=0; i<m2; i++) for(int k=0; k<m; k++) if(i&(1<<k)){
        S.at(i) = S.at(i-(1<<k))+Y.at(k);
        Sy.at(__popcount((unsigned)i)).push_back(S.at(i)); break;
    }
    for(auto &s : Sx) sort(s.begin(),s.end());
    for(auto &s : Sy) sort(s.begin(),s.end());

    long long answer = 1e18;
    for(int i=0; i<=n; i++){
        auto &A = Sx.at(i),&B = Sy.at(N/2-i);
        int pos = B.size();
        for(auto a : A){
            while(pos > 0 && a+B.at(pos-1) >= sum/2) pos--;
            if(pos) answer = min(answer,abs((sum-(a+B.at(pos-1)))-(a+B.at(pos-1))));
            if(pos < B.size()) answer = min(answer,abs((sum-(a+B.at(pos)))-(a+B.at(pos))));
        }
    }

    cout << answer << endl;
}
0