#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<bool> vb;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
template <typename T, typename U> std::istream&operator>>(std::istream&i, pair<T,U>&p) {i >> p.x >> p.y; return i;}
template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template <typename T, typename U> std::ostream&operator<<(std::ostream&o, const pair<T,U>&p) {o << p.x << ' ' << p.y; return o;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {if(t.empty())o<<'\n';for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define END '\n'
#define inf 9e18
#define ff first
#define ss second
#define pb push_back
const ll maxVal = 1e18;
struct Line {
ll m, c, idx;
Line (ll _m, ll _c, ll _idx) {
m = _m;
c = _c;
idx = _idx;
}
ll eval(ll x) {
return m * x + c;
}
ll getX(Line L) {
if (m == L.m) {
if (c > L.c) return maxVal;
else return -maxVal;
}
return (L.c - c) / (m - L.m);
}
};
struct LowerHull {
deque<Line> A;
void addLine(ll m, ll c, ll idx) {
Line now(m, c, idx);
while((int)A.size() >= 2 && now.getX(A[0]) >= A[0].getX(A[1])) A.pop_front();
A.push_front(now);
}
pair<ll, int> query(ll x) {
if (A.empty()) return {0, 0};
while((int)A.size() >= 2 && A.back().eval(x) <= A[(int)A.size() - 2].eval(x)) A.pop_back();
return {A.back().eval(x), A.back().idx};
}
};
void solve(){
int n, k;
cin >> n >> k;
vll A(n + 1), pref(n + 1, 0), suf(n + 2, 0);
for (int i = 1; i <= n; ++i) cin >> A[i], pref[i] = pref[i - 1] + A[i];
for (int i = n; i; --i) suf[i] = suf[i + 1] + A[i];
// vvll dp(n + 1, vll(k + 1, 0));
vvi par(n + 1, vi(k + 1, 0));
ll ans = 0;
int beg;
vector<LowerHull> hulls(k + 1);
for (int i = 1; i < n; ++i) {
vector<array<ll, 3>> cur(k + 1);
for (int j = 1; j <= k; ++j) {
// cout << "CURRENT: " << i << " " << j << " " << l << END;
// cout << dp[l - 1][j - 1] << " " << pref[i] << " " << pref[l - 1] << " " << suf[i + 1] << END;
ll x = suf[i + 1];
auto u = hulls[j - 1].query(x);
ll val = pref[i] * suf[i + 1] + u.ff;
// ll val = dp[l - 1][j - 1] + (pref[i] - pref[l - 1]) * (suf[i + 1]);
// cout << i << " " << j << " " << u.ff << " " << u.ss << END;
par[i][j] = u.ss;
ll m = -pref[i], c = val, idx = i;
cur[j] = {m, c, idx};
// dp[i][j] = max(dp[i][j], dp[l - 1][j - 1] + (pref[i] - pref[l - 1]) * (suf[i + 1]));
if (j == k) {
if (val >= ans) ans = val, beg = i;
}
}
for (int j = 1; j <= k; ++j) {
hulls[j].addLine(cur[j][0], cur[j][1], cur[j][2]);
}
}
// for (int i = 1; i <= n; ++i) {
// for (int j = 1; j <= k; ++j) cout << dp[i][j] << " ";
// cout << END;
// }
// cout << "LO\n";
// for (int i = 1; i <= n; ++i) {
// for (int j = 1; j <= k; ++j) cout << par[i][j] << " ";
// cout << END;
// }
// cout << "HULLS\n";
// for (int i = 1; i <= k - 1; ++i) {
// cout << "For " << i << " moves: \n";
// for (auto u: hulls[i].A) {
// cout << u.m << " " << u.c << " " << u.idx << END;
// }
// }
int idx = k;
vi fin;
while(k) fin.pb(beg), beg = par[beg][k--];
cout << ans << END << fin;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
Compilation message
sequence.cpp: In function 'void solve()':
sequence.cpp:115:9: warning: unused variable 'idx' [-Wunused-variable]
115 | int idx = k;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
1 ms |
396 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
1 ms |
388 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
1 ms |
492 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
3 ms |
748 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
2 ms |
620 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
2 ms |
748 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
3 ms |
748 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
1 ms |
492 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
1 ms |
492 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
492 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
11 ms |
1644 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
2 ms |
520 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
10 ms |
1484 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
11 ms |
1388 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
14 ms |
1524 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
11 ms |
1772 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
4 ms |
620 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
5 ms |
900 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1516 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
6 ms |
1644 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
131 ms |
10512 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
4 ms |
1516 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
76 ms |
7020 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
82 ms |
7408 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
95 ms |
8352 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
93 ms |
7532 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
88 ms |
7788 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
107 ms |
9452 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
11628 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
33 ms |
11756 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
1522 ms |
99788 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
37 ms |
12652 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
1498 ms |
97812 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
1106 ms |
69860 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
1157 ms |
80028 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
974 ms |
68092 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
948 ms |
74092 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
1415 ms |
90348 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |