結果

問題 No.3050 Prefix Removal
ユーザー GOTKAKO
提出日時 2025-03-07 23:03:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,204 bytes
コンパイル時間 2,299 ms
コンパイル使用メモリ 200,952 KB
実行使用メモリ 13,232 KB
最終ジャッジ日時 2025-03-07 23:03:42
合計ジャッジ時間 8,742 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 55
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

    int N,K; cin >> N >> K;
    priority_queue<long long,vector<long long>,greater<>> Q;
    long long answer = 0,now = 0;
    vector<long long> A(N);
    for(auto &a : A) cin >> a;
    
    for(int i=K-1; i>=0; i--){
        now += A.at(i)*(i+1);
        if(i < K-1) A.at(i) += A.at(i+1);
        if(i) Q.push(A.at(i));
    }

    answer = now;
    long long zero = 0;
    for(int i=K; i<N; i++){
        long long a = A.at(i);
        now += a*K;
        Q.push(zero);
        now += zero-Q.top(),Q.pop();
        answer = max(answer,now);
        zero -= a;
    }
    cout << answer << endl; return 0;

    vector<vector<int>> dp(N+1,vector<int>(K+1,-2e9));
    dp.at(0).at(0) = 0;
    for(int i=0; i<N; i++) for(int k=0; k<K; k++){
        if(dp.at(i).at(k) < -1e9) continue;
        int now = 0;
        for(int l=i; l<N; l++){
            now += A.at(l)*(k+1);
            dp.at(l+1).at(k+1) = max(dp.at(l+1).at(k+1),dp.at(i).at(k)+now);
        }
    }
    int answer2 = -2e9;
    for(int i=K; i<=N; i++) answer2 = max(answer2,dp.at(i).at(K));
    cout << answer2 << endl;
}  
0