This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define F first
#define S second
#define P push
#define pb push_back
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define W while
#define R return
#define C continue
#define SI size()
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define SF(x) scanf("%Id",&x)
#define SF2(x,y) scanf("%Id%Id",&x,&y)
#define SF3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define SF4(x,y,z,o) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&o)
#define all(v) v.begin(),v.end()
using namespace std;
const long long INF = 1e9+1;
const long long MOD = 1e9+7;
const int MX=100015;
int a[MX];
int n,m;
int SUM;
ll solve(){
int l=0,r=1e9+1;
int val;
W(l<r){
// l=5,r=5;
int mid=(l+r)/2;
int k=m+1;
for(int i=0;i<n;i++){
int sum=0;
W(i<n){
sum+=a[i];
if(sum>=mid){
k--;
break;
}
i++;
}
if(k==0){
val=mid;
l=mid+1;
break;
}
}
if(k){
r=mid;
}
}
ll res=0;
ll sum=0;
for(int i=0;i<n;i++){
sum+=a[i];
if(sum>=val){
res+=sum*(SUM-sum);
SUM-=sum;
sum=0;
}
}
R res;
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>a[i];
SUM+=a[i];
}
int z=SUM;
ll res=solve();
for(int i=0,j=n-1;i<j;i++,j--)
swap(a[i],a[j]);
SUM=z;
res=min(res,solve());
cout<<res;
}
Compilation message (stderr)
sequence.cpp: In function 'long long int solve()':
sequence.cpp:31:9: warning: 'val' may be used uninitialized in this function [-Wmaybe-uninitialized]
int val;
^
# | 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... |