이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=int;
const ll maxN=3e5+10;
const ll inf=1e8;
const ll mod=1e9+7;
ll n,k,a[maxN],sum=0,pre[maxN],pre2[maxN],dp[maxN][201];
ll cost(ll l,ll r)
{
return (pre[r]-pre[l-1])*(pre[r]-pre[l-1])-(pre2[r]-pre2[l-1]);
}
ll trace[maxN][201]={0};
void solve()
{
cin >> n >> k;
k++;
ll ss=0;
for(int i=1;i<=n;i++) cin >> a[i],ss+=sum*a[i],sum+=a[i];
for(int i=1;i<=n;i++)
{
pre[i]=pre[i-1]+a[i];
pre2[i]=pre2[i-1]+a[i]*a[i];
}
for(int i=1;i<=n;i++)
{
dp[i][1]=pre[i]*pre[i]-pre2[i];
}
dp[0][1]=inf;
for(int j=2;j<=k;j++)
{
for(int i=1;i<=n;i++)
{
dp[i][j]=inf;
for(int q=1;q<i;q++)
{
dp[i][j]=min(dp[i][j],dp[q-1][j-1]+cost(q,i));
if(dp[i][j]==dp[q-1][j-1]+cost(q,i))
{
trace[i][j]=q-1;
}
}
}
}
cout <<ss-dp[n][k]/2<<'\n';
ll x=n;
for(int i=k;i>1;i--)
{
cout << trace[x][i]<<' ';
x=trace[x][i];
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
# | 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... |