/**
* Author: Anil Byar
**/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// template<class T>
// using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x).size()
#define uniq(v) v.erase(unique(all(v)), v.end())
#define ft first
#define sd second
#define pb push_back
// Source: https://codeforces.com/blog/entry/68809
// { start
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// } end
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vl> vvl;
#define dbg if(0)
const ll MOD = 1e9+7;
const ll MOD9 = 998244353;
const ll INFLL = 1e18+5;
const int INF = 1e9;
void printbit(int x, int len) {string s="\n";while(len--){s=((x%2)?'1':'0')+s;x/=2;} cout<<s;}
ll power(ll x, ll y, ll mod){if (y==0) return 1;ll ret = power(x, y/2, mod);ret *= ret;ret%=mod;if (y&1) ret *= x;return ret%mod;}
ll modinv(ll x, ll mod = MOD) {return power(x, mod-2, mod);}
template<class T>
istream& operator>>(istream&in, vector<T>&v){
for (T& x:v) in>>x;
return in;
}
template<class T>
ostream& operator<<(ostream&out, vector<T>&v){
for (T& x:v) out<<x<<' ';
cout<<'\n';
return out;
}
// use ?: with brackets (?:)
// check for overflow
// think about dp
// Read the statement carefully
struct Line{
ll m, c, id;
Line(ll mm, ll cc, int idd):m(mm), c(cc), id(idd){}
Line(){m = 0;c=0;id=0;}
ll at(ll x){
return m*x+c;
}
friend ostream& operator<<(ostream&out, const Line& l){
out<<l.m<<"x + "<<l.c<<' ';
return out;
}
};
double intersection(const Line& l1, const Line& l2) {
return (l2.c-l1.c)/(double)(l1.m-l2.m);
}
bool order1(const Line& l1, const Line& l2, ll x){
return x*(l2.m-l1.m)>=l1.c-l2.c;
}
bool order2(const Line& l1, const Line& l2, const Line& l3){
return (l1.c-l2.c)*(l3.m-l2.m)>=(l2.c-l3.c)*(l2.m-l1.m);
}
#define sq(x) ((x)*(x))
const int maxN = 1e5+5; // change this
const int maxK = 8001;
void solve(){
int N, K;cin>>N>>K;
ll A[N], P[N+1]{};
for (int i = 0;i<N;i++) cin>>A[i], P[i+1] = P[i] + A[i];
vl dp1(N+1), dp2(N+1);
int prev[N+1][K+1];
memset(prev, 0, sizeof(prev));
for (int k = 1;k<=K;k++){
Line d[N+1];
int l = 0, r = 0;
d[0] = Line();
for (int i = 1;i<=N;i++){
while(r-l>0 && order1(d[l], d[l+1], P[i])) l++;
dp2[i] = d[l].at(P[i]);
prev[i][k] = d[l].id;
Line curr(P[i], dp1[i] - sq(P[i]), i);
while(r-l>0 && order2(d[r-1], d[r], curr)){
r--;
}
// if (k==K) cout<<curr<<endl;
r++;
d[r] = curr;
}
swap(dp1, dp2);
}
cout<<dp1[N]<<'\n';
vi ans;
while(K) {
ans.push_back(prev[N][K]);
N = prev[N][K];
K--;
}
reverse(all(ans));
cout<<ans;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
freopen("debug.dbg", "w", stderr);
int tt = clock();
#endif
int t=1, i = 1;
// cin>>t;
while(t--){
// cout<<"Case #"<<i++<<": ";
solve();
cout<<"\n"[t!=0];
}
#ifndef ONLINE_JUDGE
cerr << "TIME = " << (float)(clock() - tt)/CLOCKS_PER_SEC << endl;
tt = clock();
#endif
}
Compilation message
sequence.cpp: In function 'int main()':
sequence.cpp:178:14: warning: unused variable 'i' [-Wunused-variable]
178 | int t=1, i = 1;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
1 ms |
316 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
1 ms |
316 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
1 ms |
320 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
0 ms |
320 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
1 ms |
448 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
1 ms |
340 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
1 ms |
448 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
1 ms |
468 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
1 ms |
316 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
1 ms |
340 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
1 ms |
340 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
340 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
3 ms |
1088 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
1 ms |
340 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
4 ms |
1088 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
4 ms |
980 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
4 ms |
1108 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
3 ms |
1108 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
2 ms |
468 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
2 ms |
596 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
980 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
2 ms |
980 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
29 ms |
8660 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
2 ms |
980 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
26 ms |
5616 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
27 ms |
6292 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
29 ms |
6736 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
19 ms |
5720 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
24 ms |
6356 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
31 ms |
8012 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
7260 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
13 ms |
7452 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
420 ms |
84968 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
13 ms |
7764 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
501 ms |
84656 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
319 ms |
61256 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
338 ms |
65324 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
257 ms |
54308 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
302 ms |
61324 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
398 ms |
77068 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |