This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
using namespace std;
void solve(long long N, long long K, vector<long long> a) {
long long tval = 0;
for (long long i=0;i<N;i++) {
tval += a[i];
}
tval = tval*tval;
deque<long long> d[K+1];
long long dsums[K+1];
for (long long i=0;i<=K;i++) {
dsums[i]=0;
}
for (long long i=0;i<N;i++) {
d[0].push_back(a[i]);
dsums[0] += a[i];
}
for (long long t=0;t<(2*N);t++) {
for (long long i=0;i<K;i++) {
while (1) {
long long r = d[i].back();
if (max(dsums[i]-r,dsums[i+1]+r)<max(dsums[i],dsums[i+1])) {
d[i].pop_back();
d[i+1].push_front(r);
dsums[i]=dsums[i]-r;
dsums[i+1]=dsums[i+1]+r;
} else {
break;
}
}
}
}
for (long long i=0;i<=K;i++) {
tval -= dsums[i]*dsums[i];
/*cout << "i="<<i<<", dsums="<<dsums[i]<<"\n";
for (long long x: d[i]) {
cout << "term: "<<x<<"\n";
}*/
}
tval = tval/2;
cout << tval;
}
int main() {
long long N,K; cin >> N >> K;
vector<long long> a;
for (long long i=0;i<N;i++) {
long long x; cin >> x;
if (x != 0) {
a.push_back(x);
}
}
K = min(K,(long long) (a.size()-1));
solve(a.size(),K,a);
}
# | 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... |