제출 #856773

#제출 시각아이디문제언어결과실행 시간메모리
856773Amirreza_Fakhri수열 (APIO14_sequence)C++17
49 / 100
2073 ms80672 KiB
// In the name oF the God 
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define halt(x) cout << x << '\n', exit(0)
using namespace std;

const int inf = 1e9 + 7;
const int mod = 998244353;
const int maxN = 1e5 + 5, maxK = 2e2 + 5;

int n, k, ps[maxN], dp[maxN];
int32_t opt[maxK][maxN];

int cost(int l, int r) {
    return ps[l] * (ps[r] - ps[l]);
}

void f(int pos, int q) {
    if (!q) return;
    cout << pos << ' ';
    f(opt[q][pos], q - 1);
}

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> k; k++;
    for (int i = 1; i <= n; i++) {
        cin >> ps[i]; ps[i] += ps[i-1];
    }
    for (int i = 1; i <= n; i++) dp[i] = -inf * inf;
    dp[0] = 0;
    for (int i = 1; i <= k; i++) {
        opt[i][n+1] = n-1;
        for (int j = n; j >= i; j--) {
            dp[j] = -inf * inf;
            for (int o = opt[i-1][j]; o <= min(j - 1, 1ll * opt[i][j+1]); o++) {
                if (dp[j] < dp[o] + cost(o, j)) {
                    dp[j] = dp[o] + cost(o, j);
                    opt[i][j] = o;
                }
            }
        }
    }
    cout << dp[n] << '\n';
    f(opt[k][n], k - 1);
    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...