이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("bmi,bmi2,lzcnt,popcnt")
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
//#pragma expected_value
//#pragma isolated_call
//#pragma disjoint
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define int long long
//#define double long double
#define Fi first
#define Se second
#define Rep(i,a,b) for (int i=a;i<=b;++i)
#define Repu(i,b,a) for (int i=b;i>=a;--i)
#define pb push_back
#define ms(a,i) memset(a,i,sizeof(a))
#define sz size()
#define mp make_pair
#define endl '\n'
#define sef setprecision(6)<<fixed
#define cer cout<<"cak"<<endl;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<double> va;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<va> vva;
//const double EPS=1e-9;
const double PI=acos(-1);
const long long oo=1e18;
const int MN=3e5+5;
const int mod=1e9+7;
using cd=complex<double>;
//typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> index_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,k;
int a[MN];
struct Node {
int pref =0, suff = 0;
int sum = 0;
int ans = 0;
};
Node base;
Node merge(Node a, Node b) {
Node c;
c.ans = max(a.ans, b.ans);
c.ans = max(c.ans, a.suff + b.pref);
c.pref = max(a.pref, a.ans + b.pref); c.suff = (b.suff, a.suff + b.sum);
c.sum = a.sum + b.sum;
return c;
}
Node st[4*MN];
void build(int id,int l,int r) {
if(l==r) {
st[id].sum = a[l];
int temp = 0;
if(temp < a[l]) temp = a[l];
st[id].ans = temp; st[id].pref = temp; st[id].suff = temp;
return;
}
int mid = (l+r)/2;
build(id*2,l,mid); build(id*2+1,mid+1,r);
st[id] = merge(st[id*2], st[id*2+1]);
}
Node get(int id,int l,int r,int u,int v)
{
if(l>v or r<u) return base;
if(u<=l and v>=r) return st[id];
int mid = (l+r)/2;
return merge(get(id*2,l,mid,u,v), get(id*2+1,mid+1,r,u,v));
}
int dp_before[MN], dp_now[MN];
void compute(int l,int r,int tl,int dr) {
if(l>r) return;
int mid = (l+r)/2;
ii maxx = {dp_before[mid], mid};
Rep(i,tl,min(dr,mid-1)) {
int temp = get(1,1,n,i+1,mid).ans;
maxx = max(maxx, ii(dp_before[i] + temp,i));
}
dp_now[mid] = maxx.Fi;
compute(l,mid-1,tl,maxx.Se); compute(mid+1,r,maxx.Se,dr);
}
signed main() {
//freopen(".inp","r",stdin); freopen(".out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>k;
if(n<=2000) {
Rep(i,1,n) cin>>a[i];
build(1,1,n);
Rep(i,1,n) dp_before[i] = get(1,1,n,1,i).ans;
Rep(step,2,k) {
Rep(i,1,n) {
dp_now[i] = dp_before[i];
Rep(j,1,i) dp_now[i] = max(dp_now[i], dp_before[j-1] + get(1,1,n,j,i).ans);
}
Rep(i,1,n) dp_before[i] = dp_now[i];
}
cout<<dp_before[n];
return 0;
}
int cnt = 0;
Rep(i,1,n) {
cin>>a[i];
if(a[i] < 0) cnt++;
}
if(cnt == 0) {
int sum = 0;
Rep(i,1,n) {
sum += a[i];
}
cout<<sum;
return 0;
}
else {
if(k==1) {
int ans = 0;
Rep(i,1,n) {
dp_now[i] = max(dp_now[i-1] + a[i], a[i]);
ans = max(ans,dp_now[i]);
}
cout<<ans;
}
else {
int sum = 0;
Rep(i,1,n) {
if(a[i]<0) continue;
sum += a[i];
}
cout<<sum;
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
feast.cpp: In function 'Node merge(Node, Node)':
feast.cpp:62:52: warning: left operand of comma operator has no effect [-Wunused-value]
62 | c.pref = max(a.pref, a.ans + b.pref); c.suff = (b.suff, a.suff + b.sum);
| ~~^~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |