이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
int trace[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 - 1; 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];
if(dp[i][j] <= dp[i - 1][u - 1] + front * back)
{
dp[i][j] = dp[i - 1][u - 1] + front * back;
trace[i][j] = u - 1;
}
}
int pos = 0;
ll mx = -1e17;
for(int i = 1; i <= n; i++)
if(mx <= dp[k][i]) mx = dp[k][i], pos = i;
deque<int> res;
for(int i = k; i >= 1; i--)
{
res.push_front(pos);
pos = trace[i][pos];
assert((int)res.size() <= k);
}
cout << mx << '\n';
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |