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<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<math.h>
#include<assert.h>
using namespace std;
const double PI = (double) atan(1.0) * 4;
const int64_t INF = (int64_t) 1e18 + 777;
const int64_t mINF = (int64_t) 2e9 + 777;
const int64_t offset = (int64_t) 1e6 + 777;
const int64_t MOD = 1e9 + 7;
const int nbit = 11;
const int ndig = 10;
const int nchar = 26;
const int p1 = 31;
const int p2 = 53;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
// 0 right // 1 down // 2 left // 3 up
bool Q = false;
int modadd(int t1, int t2)
{
int res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return (res % MOD);
}
struct Line
{
int64_t k; int64_t m;
int id;
mutable int64_t p;
bool operator < (const Line& o) const
{
if(Q) return p < o.p;
return k < o.k;
}
};
struct LineContainer : multiset<Line, less<> >
{
int64_t div(int64_t a, int64_t b)
{
return a / b - ((a ^ b) < 0 && (a % b));
}
bool isect(iterator x, iterator y)
{
if(y == end())
{
x->p = INF;
return false;
}
if(x->k == y->k)
{
if(x->m > y->m) x->p = INF;
else x->p = -INF;
}
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(int64_t k, int64_t m, int i)
{
auto z = insert({k, m, i, 0});
auto y = z++;
auto x = y;
while(isect(y, z)) z = erase(z);
if(x != begin() && isect(--x, y)) erase(x, y = erase(y));
while((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y));
}
pair<int64_t, int> query(int64_t x)
{
assert(!empty());
Q = true;
auto l = *lower_bound({0, 0, 0, x});
Q = false;
return {l.k * x + l.m, l.id};
}
};
struct Solution
{
int n; int k;
vector<int> a;
Solution() {}
void solve()
{
cin >> n >> k;
a.resize(n);
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
vector<int64_t> prefix(n + 1, 0);
vector<int64_t> suffix(n + 2, 0);
for(int i = 1; i <= n; i++)
{
prefix[i] = prefix[i - 1] + a[i - 1];
}
for(int i = n; i >= 1; i--)
{
suffix[i] = suffix[i + 1] + a[i - 1];
}
vector<int64_t> dp(n + 1, -INF);
vector<int64_t> f(n + 1, -INF);
vector<vector<int> > trace(k, vector<int>(n + 1, -1));
dp[0] = 0;
int e = -1;
int64_t ans = -INF;
for(int t = 0; t < k; t++)
{
LineContainer lc;
for(int i = 0; i <= n; i++)
{
f[i] = dp[i];
int64_t p = prefix[i];
int64_t suf = suffix[i + 1];
if(!lc.empty())
{
pair<int64_t, int> pr = lc.query(suf);
int64_t val = pr.first;
int id = pr.second;
f[i] = max(f[i], val + p * suf);
trace[t][i] = id;
}
if(ans < f[i])
{
ans = f[i];
e = i;
}
lc.add(-p, dp[i], i);
}
swap(f, dp);
}
cout << ans << "\n";
vector<int> path;
for(int i = k - 1; i >= 0; i--)
{
path.push_back(e);
e = trace[i][e];
}
reverse(path.begin(), path.end());
for(int i = 0; i < k; i++)
{
cout << path[i] << " ";
}
}
int modsub(int t1, int t2)
{
int res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
bool BIT(int& mask, int& j)
{
return (mask & MASK(j));
}
int64_t Abs(int64_t t1)
{
if(t1 < 0) return -t1;
return t1;
}
int MASK(int j)
{
return (1 << j);
}
};
void __startup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__startup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
return 0;
}
# | 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... |