This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
template<class A, class B>
bool maximize(A &a, const B& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<class A, class B>
bool minimize(A &a, const B& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
using namespace std;
constexpr int MAX_N = 105, MAX_K = 1E4 + 5, INF = 0X3F3F3F3F;
int N, L, R, K, A[MAX_N];
bool visited[MAX_N][MAX_N][MAX_K];
signed result = INF, minimum[MAX_N][MAX_N][MAX_K], leftCost[MAX_N][MAX_K], rightCost[MAX_N][MAX_K];
long long findRightOptimalCost(const int i, const int j, const int k) {
if (k < 0)
return INF;
if (i > R)
return 0;
if (!visited[i][j][k]) {
visited[i][j][k] = true;
minimum[i][j][k] = findRightOptimalCost(i + 1, j, k) + A[i];
if (j <= N) {
const int d = j - i;
minimize(minimum[i][j][k], findRightOptimalCost(i, j + 1, k));
if (k >= d)
minimize(minimum[i][j][k], findRightOptimalCost(i + 1, j + 1, k - d) + A[j]);
}
}
return minimum[i][j][k];
}
long long findLeftOptimalCost(const int i, const int j, const int k) {
if (k < 0)
return INF;
if (i < L)
return 0;
if (!visited[i][j][k]) {
visited[i][j][k] = true;
minimum[i][j][k] = findLeftOptimalCost(i - 1, j, k) + A[i];
if (j >= 1) {
const int d = i - j;
minimize(minimum[i][j][k], findLeftOptimalCost(i, j - 1, k));
if (k >= d)
minimize(minimum[i][j][k], findLeftOptimalCost(i - 1, j - 1, k - d) + A[j]);
}
}
return minimum[i][j][k];
}
signed main() {
#ifdef LOCAL
freopen("input.INP", "r", stdin);
#endif // LOCAL
cin.tie(0) -> sync_with_stdio(0);
cout.tie(0);
cin >> N >> L >> R >> K;
for (int i = 1; i <= N; ++i)
cin >> A[i];
for (int i = L, k; i <= R; ++i)
for (k = 0; k <= K; ++k)
leftCost[i][k] = findLeftOptimalCost(i, L - 1, k);
memset(visited, false, sizeof(visited));
for (int i = L, k; i <= R; ++i)
for (k = 0; k <= K; ++k)
rightCost[i][k] = findRightOptimalCost(i, R + 1, k);
minimize(result, min(leftCost[R][K], rightCost[L][K]));
for (int i = L, k; i < R; ++i)
for (k = 0; k <= K; ++k)
minimize(result, leftCost[i][k] + rightCost[i + 1][K - k]);
cout << result << '\n';
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... |