結果
| 問題 |
No.1248 Kth Sum
|
| コンテスト | |
| ユーザー |
tute7627
|
| 提出日時 | 2020-07-19 15:32:12 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 71 ms / 2,000 ms |
| コード長 | 1,410 bytes |
| コンパイル時間 | 2,157 ms |
| コンパイル使用メモリ | 200,052 KB |
| 最終ジャッジ日時 | 2025-01-12 00:52:39 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 36 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define ALL(a) (a).begin(),(a).end()
#define ALLR(a) (a).rbegin(),(a).rend()
#define rep(i,n,m) for(int i = (n); i < (int)(m); i++)
#define rrep(i,n,m) for(int i = (m) - 1; i >= (int)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const int INF = (int)1e9+1;
template<typename T>
bool chmin(T &a,T b){if(a>b){a=b;return true;}else return false;}
template<typename T>
bool chmax(T &a,T b){if(a<b){a=b;return true;}else return false;}
template<typename T>
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n,k;
cin>>n>>k;
assert(1<=n&&n<=200000);
assert(1<=k&&k<=n);
vector<int>a(n);
rep(i,0,n){
cin>>a[i];
assert(1<=a[i]&&a[i]<=(int)1e9);
}
if(k==1){
cout<<a[0]<<endl;
return 0;
}
priority_queue<int>que;
ll sum=0;
ll res=INF;
vector<int>b(n,INF);
b[n-1]=a[n-1];
rrep(i,0,n-1)b[i]=min(b[i+1],a[i]);
rrep(i,k-1,n){
int m=(i+k-2)/(k-1);
if(m==1){
chmin(res,(ll)a[i]);
}
else if(m*k<=n){
while(que.size()>m-1){
sum-=que.top();
que.pop();
}
if(b[m*k-1]<=que.top())chmin(res,a[i]+sum);
else chmin(res,a[i]+sum-que.top()+b[m*k-1]);
}
que.push(a[i]);
sum+=a[i];
}
cout<<res<<endl;
return 0;
}
tute7627