This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Coded by thegamercoder
Happy Competition and Hacking
*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define M_PI 3.14159265358979323846
#define FILER 0
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = pow(10, 9) + 7;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const ull INFUL = 0x3f3f3f3f3f3f3f3f;
const ll INFT = 0x3f3f3f3f;
const ll MAX = 401;
const ll MODD = 998244353;
const ll BL = 1;
const double EPS = 1e-10;
#define V vector
#define pll pair<ll, ll>
#define pull2 pair<ull,ull>
#define MS multiset
#define M map
#define Q queue
#define PQ priority_queue
#define IOF ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define FOR(typ,i,a,b,c) for(typ i = a; i < b; i += c)
#define FORR(typ,i,a,b,c) for(typ i = a; i > b; i -= c)
#define FORA(a,i) for(auto i : a)
#define FORAE(a,i) for(auto &i : a)
#define all(v) v.begin(),v.end()
#define sorta(a) sort(all(a))
#define sortd(a) sort(all(a), greater<ll>())
#define setp(x) setprecision(x)<<fixed
#define RET return
#define log(a,b) log(b)/log(a)
#define WH(s) while(s)
#define WHI(t) WH(t--)
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define Yes cout<<"Yes"<<endl;
#define No cout<<"No"<<endl;
#define YESNO(s) cout<<(s?"YES":"NO")<<endl;
#define YesNo(s) cout<<(s?"Yes":"No")<<endl;
#define TYP 0
using namespace std;
V<ll> a, pf, res, dp, ndp;
V<V<int>> p;
//V<V<ll>> fdp;
ll n, k;
struct line
{
mutable ll k, b, p, i;
bool operator<(const line& o) const {
RET k < o.k;
}
bool operator<(ll x)const
{
RET p < x;
}
};
struct lineCont : multiset<line, less<>>
{
ll div(ll a, ll b) { RET a / b - ((a ^ b) < 0 && (a % b)); }
bool sect(iterator a, iterator b)
{
if (b == end()) { a->p = INFL; RET 0; }
if (a->k == b->k) a->p = a->b > b->b ? INFL : -INFL;
else a->p = div(b->b - a->b, a->k - b->k);
RET a->p >= b->p;
}
void add(ll k, ll b, ll id)
{
auto z = insert({ k,b,0,id }), y = z++, x = y;
WH(sect(y, z)) z = erase(z);
if (x == begin())RET;
if (sect(--x, y))sect(x, y = erase(y));
WH((y = x) != begin() && (--x)->p >= y->p) sect(x, erase(y));
}
pll query(ll x)
{
auto l = *lower_bound(x);
RET{ l.k * x + l.b, l.i };
}
};
void solve()
{
cin >> n >> k;
a.resize(n + 1); pf.resize(n + 1);
FOR(ll, i, 1, n + 1, 1)cin >> a[i], pf[i] = pf[i - 1] + a[i];
dp.resize(n + 1); // dp[i][j-1]
p.resize(n + 1, V<int>(k + 1)); //fdp.resize(n + 1, V<ll>(k + 1));
lineCont cht;
// pf[c] (m) * pf[i] (x) + (dp[c][j-1] - pf[c] * pf[c]) (c)
FOR(ll, j, 1, k + 1, 1)
{
ndp.assign(n + 1, 0);
FOR(ll, i, 1, n + 1, 1)
{
if (i > 1)
{
auto l = cht.query(pf[i]);
ndp[i] = l.first;
p[i][j] = l.second;
//fdp[i][j] = ndp[i];
}
cht.add(pf[i], dp[i] - pf[i] * pf[i], i);
}
swap(ndp, dp);
}
cout << dp[n] << endl;
ll i = n, j = k;
WH(j)
{
i = p[i][j]; j--;
cout << i << ' ';
}
cout << endl;
}
void init()
{
if (FILER)
{
freopen("pump.in", "r", stdin);
freopen("pump.out", "w", stdout);
}
}
void handle()
{
init();
ll t = 1;
if (TYP)cin >> t;
WHI(t)solve();
}
int main()
{
IOF
handle();
RET 0;
}
Compilation message (stderr)
sequence.cpp: In function 'void init()':
sequence.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen("pump.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen("pump.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... |