Submission #681741

#TimeUsernameProblemLanguageResultExecution timeMemory
681741vjudge1수열 (APIO14_sequence)C++14
28 / 100
2068 ms16084 KiB
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef pair<int, int> ii;

void print() {cerr << '\n';} template <typename T1, typename... T2>
void print(const T1 &a, const T2 &...b) { cerr << a << ' ', print(b...); }

const int N = 1e4 + 5;
const int mod = 1e9 + 7;
int n, k;
ll a[N];
ll dp[205][N];

void solve()
{
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
        cin >> a[i], a[i] += a[i - 1];
    
    for(int i = 0; i <= k; i++)
        for(int j = 0; j <= n; j++) 
            dp[i][j] = -1e17;
    
    dp[0][0] = 0;
    for(int i = 1; i <= k; i++)
        for(int j = n; j >= i; j--) // ket thuc tai j
            for(int u = j; u >= i; u--) // bat dau tai u
            {
                ll back = a[n] - a[j];
                ll front = a[j] - a[u - 1];
                dp[i][j] = max(dp[i][j], dp[i - 1][u - 1] + front * back);
            }

    int pos = 0;
    ll mx = -1e17;
    for(int i = 1; i <= n; i++)
        if(mx <= dp[k][i]) mx = dp[k][i], pos = i;
        
    cout << mx << '\n';
    deque<int> res;
    for(int i = k; i >= 1; i--)
    {
        res.push_front(pos);
        for(int j = pos; j >= i; j--)
        {
            ll back = a[n] - a[pos];
            ll front = a[pos] - a[j - 1];
            if(dp[i][pos] == dp[i - 1][j - 1] + front * back)
            {
                pos = j - 1;
                break;
            }
        }
    }
    for(const int &x : res) cout << x << ' ';
}

signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    while(t--) solve();
    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...