This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e5 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n, k; int trace[MAXN][202];
ll dp[MAXN], a[MAXN];
struct convexhulltrick{
struct point {
ll x, y, id;
ll dot(point p) {
return x * p.x + y * p.y;
}
ll cross(point p) {
return x * p.y - y * p.x;
}
point conj() {
return {-y, x};
}
point vt(point p) {
return {x - p.x, y - p.y};
}
};
vector<point> hull, vec;
void add(ll a, ll b, int id) {
auto p = point{a, b, id};
while (vec.size() >= 1 && vec.back().dot(p.vt(hull.back())) <= 0) { // <= 0 neu max
vec.pop_back();
hull.pop_back();
}
if (!hull.empty())
vec.push_back(p.vt(hull.back()).conj());
hull.push_back(p);
}
ii get(ll x) {
auto query = point{x, 1};
auto it = lower_bound(vec.begin(), vec.end(), query, [](point a, point b) {
return a.cross(b) < 0; // <0 neu max
});
return {query.dot(hull[it - vec.begin()]), hull[it - vec.begin()].id};
}
void clear(){
hull.clear(), vec.clear();
}
} cvh;
void Solve() {
cin >> n >> k;
For(i, 1, n) cin >> a[i], a[i] += a[i - 1];
reset(dp, -0x3f); dp[0] = 0; cvh.add(0, 0, 0);
For(_k, 1, k){
For(i, 1, n) {
ll dpi = dp[i];
ii tmp = cvh.get(a[n] - a[i]);
dp[i] = tmp.fi + a[i] * (a[n] - a[i]);
cvh.add(-a[i], dpi, i);
trace[i][_k] = tmp.se;
}
cvh.clear();
}
ll ans = 0, i = 0;
int _k = k;
For(j, 1, n) if(maximize(ans, dp[j])) i = j;
vector<int> List;
while(i){
List.push_back(i);
i = trace[i][_k];
--_k;
}
cout << ans << '\n';
reverse(all(List));
if(ans == 0){
For(i, 1, k) List.push_back(i);
}
Forv(x, List) cout << x << ' ';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("APIO14_sequence.inp", "r")) {
freopen("APIO14_sequence.inp", "r", stdin);
freopen("APIO14_sequence.out", "w", stdout);
}
int t = 1;
// cin >> t;
for (int test = 1; test <= t; test++) {
Solve();
}
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int32_t main()':
sequence.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen("APIO14_sequence.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen("APIO14_sequence.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |