제출 #41627

#제출 시각아이디문제언어결과실행 시간메모리
41627funcsr수열 (APIO14_sequence)C++14
0 / 100
1 ms488 KiB
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cassert>
#include <map>
using namespace std;
#define int long long
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define index(x, y) (int)(lower_bound(all(x), y) - x.begin())
#define _1 first
#define _2 second
#define pb push_back
#define MOD 1000000007
#define INF (1LL<<60)

int N, K;
int B[100001];
long long dp[100001][202];
int pre[100001][202];
signed main() {
  cin >> N >> K;
  K++;
  rep(i, N) cin >> B[i+1];
  rep(i, N) B[i+1] += B[i];
  rep(i, N+1) rep(j, K+1) dp[i][j] = INF;
  dp[0][0] = 0;
  for (int x=1; x<=N; x++) {
    rep(k, K) {
      rep(y, x) {
        long long v = 1LL*B[x]*B[x] + (dp[y][k] + 1LL*B[y]*B[y] - 2LL*B[x]*B[y]);
        if (dp[x][k+1] > v) {
          dp[x][k+1] = v;
          pre[x][k+1] = y;
        }
      }
    }
  }
  long long m = (1LL*B[N]*B[N]-dp[N][K])/2;
  cout << m << "\n";
  int p = pre[N][K], k = K-1;
  vector<int> seq;
  while (k > 0) {
    seq.pb(p);
    p = pre[p][k];
    k--;
  }
  reverse(all(seq));
  for (int x : seq) cout << x << " "; cout << "\n";
  return 0;
}
#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...