이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
///
/// ♪ Pizza mozzarella rella rella rella... ♪
///
#define _USE_MATH_DEFINES
#define FAST ios::sync_with_stdio(false),cin.tie(0);
#include <bits/stdc++.h>
#define Loop(x, l, r) for(int x = (l); x < (r); ++x)
#define LoopR(x, l, r) for(int x = (r)-1; x >= (l); --x)
#define all(x) x.begin(), x.end()
#define Kill(x) exit((cout << (x) << '\n', 0))
#define YN(flag) ((flag)? "YES": "NO")
#define F first
#define S second
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
typedef std::pair<ll ,ll > pll;
using namespace std;
const int N = 100'010;
const int K = 210;
ll sum[K], pro[N];
ll a[N];
int ans[K];
int n, k;
void add(int i, ll x){pro[i] += x*sum[i]; sum[i] += x;}
void rem(int i, ll x){sum[i] -= x; pro[i] -= x*sum[i];}
void frw(int i)
{
if(i == k) return;
for(;;)
{
frw(i+1);
if(ans[i] == ans[i+1]-1) return;
ll x1 = pro[i-1] + pro[i];
rem(i, a[ans[i]]);
add(i-1, a[ans[i]]);
++ans[i];
ll x2 = pro[i-1] + pro[i];
if(x1 < x2)
{
--ans[i];
rem(i-1, a[ans[i]]);
add(i, a[ans[i]]);
return;
}
}
}
int main()
{
FAST;
cin >> n >> k; ++k;
Loop(i,0,n) cin >> a[i];
ans[k] = n;
Loop(i,0,k) ans[i] = i, sum[i] = a[i];
Loop(i,k,n) add(k-1, a[i]);
frw(1);
ll sum = 0, sum2 = 0;
Loop(i,0,n) sum += a[i], sum2 += a[i]*a[i];
ll fans = (sum*sum-sum2)/2;
Loop(i,0,k) fans -= pro[i];
cout << fans << '\n';
Loop(i,1,k) cout << ans[i] << ' ';
}
# | 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... |