제출 #446085

#제출 시각아이디문제언어결과실행 시간메모리
446085aryan12수열 (APIO14_sequence)C++17
0 / 100
2074 ms4300 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

void Solve() {
	int n, k;
	cin >> n >> k;
	int a[n + 1];
	a[0] = 0;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		a[i] += a[i - 1];
	}
	int dp[n + 1][k + 1];
	for(int i = 0; i <= n; i++) {
		for(int j = 0; j <= k; j++) {
			dp[i][j] = -1;
		}
	}
	dp[0][0] = 0;
	int ans = -1;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= k; j++) {
			if(j > i) {
				continue;
			}
			for(int prev = 0; prev < i; prev++) {
				if(dp[prev][j - 1] != -1) {
					dp[i][j] = max(dp[i][j], dp[prev][j - 1] + (a[i] - a[prev]) * (a[n] - a[i]));
				}
			}
			if(j == k) {
				ans = max(ans, dp[i][k]);
			}
		}
	}
	cout << ans << "\n";
}

int32_t main() {
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\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...