제출 #1084986

#제출 시각아이디문제언어결과실행 시간메모리
1084986mingga수열 (APIO14_sequence)C++17
0 / 100
57 ms131160 KiB
#include "bits/stdc++.h"

using namespace std;

#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 2e18
#define fast_cin()                  \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)
#define out(file) freopen(file, "w", stdout)
#define in(file) freopen(file, "r", stdin)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
int MOD = 1e9 + 7;
const int N = 1e5;
int n, k, a[N], trace[N][205], ps[N];

vector<int> pre, cur;

void calc(int l, int r, int optl, int optr, int k) {
  if(l > r) return;
  pair<int, int> best = {-inf, -1};
  int mid = (l + r) >> 1;
  for(int i = optl; i <= min(mid-1, optr); i++) {
    best = max(best, {pre[i] + ps[i] * (ps[mid]-ps[i]), i});
  }
  trace[mid][k] = best.se;
  cur[mid] = best.fi;
  calc(l, mid-1, optl, best.se, k);
  calc(mid+1, r, best.se, optr, k);
}

signed main() {
    fast_cin();
    cin >> n >> k;
    for(int i = 1; i <= n; i++) cin >> a[i], ps[i] = ps[i-1] + a[i];
    pre.resize(n+1, 0);
    cur.resize(n+1, 0);
    for(int i = 1; i <= k; i++) {
      calc(i, n, i, n, i);
      pre = cur;
    }
    cout << cur[n] << ln;
    vector<int> ans;
    int cur = n;
    for(int i = k; i > 0; i--) {
      ans.pb(trace[cur][i]);
      cur = trace[cur][i] - 1;
    }
    reverse(all(ans));
    for(int x : ans) cout << x << ' ';
    cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...