# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1102905 | 0pt1mus23 | Feast (NOI19_feast) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ins insert
#define pb push_back
#define endl '\n'
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
const int mod = 1e9 +9, sze = 3e5 +5, inf = INT_MAX, LL = 30;
int arr[sze];
int n,k;
struct yapi{
pair<int,pair<int,int>> left ={-inf,{-inf,inf}};
pair<int,pair<int,int>> right={-inf,{-inf,inf}};
pair<int,pair<int,int>> total={-inf,{-inf,inf}};
pair<int,pair<int,int>> hepsi={-inf,{-inf,inf}};
};
yapi T[sze*4];
/*
pairlerden zehlem gedir
C.LEFT : A.LEFT | A.HEPSI + B.LEFT
C.RIGHT : B.RIGHT | A.RIGHT + B.HEPSI
+ C.HEPSI : A.HEPSI + B.HEPSI
C.TOTAL : C.LEFT | C.RIGHT | C.HEPSI | A.TOTAL | B.TOTAl | A.RIGHT + B.LEFT
*/
pair<int,pair<int,int>> ekle(pair<int,pair<int,int>> a, pair<int,pair<int,int>> b){
pair<int,pair<int,int>> c;
c.first = a.first + b.first;
c.second = {a.second.first, b.second.second};
return c;
}
yapi combine(yapi a, yapi b){
yapi c;
c.hepsi = ekle(a.hepsi, b.hepsi);
c.left = max(a.left, ekle(a.hepsi,b.left));
c.right = max(b.right, ekle(a.right,b.hepsi));
c.total = a.total;
c.total = max(c.left,c.right);
c.total = max(c.total,c.hepsi);
c.total = max(c.total,a.total);
c.total = max(c.total,b.total);
c.total = max(c.total,ekle(a.right,b.left));
// cout<<b.left.second.first<<" "<<b.left.second.second<<endl;
// cout<<c.total.second.first<<" "<<c.total.second.second<<endl;
return c;
}
void build(int node,int l,int r){
if(l==r){
T[node].left = {arr[l],{l,l}};
T[node].right = {arr[l],{l,l}};
T[node].hepsi = {arr[l],{l,l}};
T[node].total = {arr[l],{l,l}};
return;
}
int mid = (l+r)/2;
build(2*node,l,mid);
build(2*node +1,mid+1,r);
// cout<<l<<" "<<r<<" comb : \n";
T[node]=combine(T[node*2],T[node*2 +1]);
// cout<<l<<" "<<r<<" : "<<T[node].total.first<<endl;
}
yapi qry(int node,int l,int r,int lx,int rx){
if(lx>r || rx<l){
yapi sozel;
sozel.left ={-inf,{0,0}};
sozel.right={-inf,{0,0}};
sozel.total={-inf,{0,0}};
sozel.hepsi={-inf,{0,0}};
return sozel;
}
if(lx>=l && rx<=r){
return T[node];
}
int mid = (lx+rx)/2;
yapi a = qry(2*node,l,r,lx,mid);
yapi b = qry(2*node +1,l,r,mid+1,rx);
return combine(a,b);
}
pair<int,pair<int,int>> qry(int l,int r){
return qry(1,l,r,0,n-1).total;
}
vector<pair<int,pair<int,int>>> lst;
void DaDONTc(int l,int r){
if(l>r){
return;
}
pair<int,pair<int,int>> tot = qry(l,r);
if(tot.first<=0){
return;
}
lst.pb(tot);
int lx = tot.second.first;
int rx = tot.second.second;
// cout<<"used : "<<tot.first<<" "<<lx<<" "<<rx<<endl;
int ll =l;
int lr = lx-1;
int rl = rx+1;
int rr = r;
DaDONTc(ll,lr);
DaDONTc(rl,rr);
}
void hiz(){
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>arr[i];
}
build(1,0,n-1);
// cout<<qry(0,2).second.second<<endl;
// return;
DaDONTc(0,n-1);
sort(all(lst));
int ans=0;
vector<pair<int,int>> used;
while(!lst.empty() && k){
ans+=lst.back().first;
used.pb({lst.back().second.first,lst.back().second.second});
--k;
lst.pop_back();
}
// cout<<ans<<endl;
multiset<int> sil;
for(auto v:used){
// cout<<v.first<<" "<<v.second<<endl;
for(int i =v.first;i<=v.second;i++){
if(arr[i]<0){
sil.ins(-arr[i]);
}
}
}
while(k-- && !sil.empty()){
ans+=(*sil.rbegin());
sil.erase(--sil.end());
}
putr(ans);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int tt = 1;
// cin>>tt;
while(tt--){
hiz();
}
return 0;
}