#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define Rep(i, n) for(int i = 1; i <= n; ++i)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define Forv(v, h) for(auto &v : h)
#define Bit(x, i) ((x) >> (i) & 1ll)
#define onbit(x, i) ((x) (1ll << i))
#define offbit(x, i) ((x) &~ (1ll << i))
#define cnt_bit(x) __builtin_popcountll(x)
#define Log2(x) (63 - __builtin_clzll(x))
#define all(h) h.begin(), h.end()
#define reset(h, v) (memset(h, v, sizeof(h)))
#define memoryof(v) (sizeof(v) / 1024 / 1024)
using ii = pair<int, int>;
using ull = unsigned long long;
using db = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using vii = vector<ii>;
const int dx[] = {0, 0, +1, -1};
const int dy[] = {-1, +1, 0, 0};
const int MAXN = 1e2 + 10;
const int MOD = 1e9 + 7;
const int oo = 1e18 + 1;
const int base = 311;
template <class X, class Y> bool maximize(X &a, const Y &b) {
if(a < b) return a = b, true;
return false;
}
template <class X, class Y> bool minimize(X &a, const Y &b) {
if(a > b) return a = b, true;
return false;
}
// (x, y) -> (u, v) = Ckn(u - x, x + y - u - v);
// Ckn(k, a) * Ckn(k, b) = C(a, a + b); (k : 1 -> min(a, b))
void fixmod(int &val) {
if (val < 0) val += MOD*MOD;
val %= MOD;
}
int n, l, r, k, a[MAXN], id1 = 0, id2 = 0;
ii b[MAXN], c[MAXN];
vvvi dp;
int solve(int i, int j, int sum)
{
if (i > (r - l + 1)) return 0;
if (~dp[i][j][sum]) return dp[i][j][sum];
int cur = +oo;
minimize(cur, solve(i + 1, j, sum) + b[i].fi);
if (j <= id2) minimize(cur, solve(i, j + 1, sum));
if (j <= id2 && sum + abs(b[i].se - c[j].se) <= k)
minimize(cur, solve(i + 1, j + 1, sum + abs(b[i].se - c[j].se)) + c[j].fi);
return dp[i][j][sum] = cur;
}
void Progess()
{
cin >> n >> l >> r >> k;
For(i, 1, n) {
cin >> a[i];
if (l <= i && i <= r) b[++id1] = {a[i], i};
else c[++id2] = {a[i], i};
}
dp.assign(id1+5, vvi(id2+5, vi(k + 5, -1)));
cout << solve(1, 1, 0) << '\n';
}
signed main(void) {
ios_base::sync_with_stdio(false);cin.tie(nullptr);
cout.tie(nullptr);
#define task "holding"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}//_______________________________
Progess();
cerr << "\nTime elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s.\n";
return (0 ^ 0);
}
Compilation message
holding.cpp: In function 'int main()':
holding.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
holding.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
10 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
10 ms |
10576 KB |
Output is correct |
8 |
Correct |
1 ms |
1020 KB |
Output is correct |
9 |
Correct |
2 ms |
1016 KB |
Output is correct |
10 |
Correct |
2 ms |
1104 KB |
Output is correct |
11 |
Correct |
3 ms |
2044 KB |
Output is correct |
12 |
Correct |
1 ms |
848 KB |
Output is correct |
13 |
Correct |
52 ms |
73032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
10 ms |
10576 KB |
Output is correct |
8 |
Correct |
1 ms |
1020 KB |
Output is correct |
9 |
Correct |
2 ms |
1016 KB |
Output is correct |
10 |
Correct |
2 ms |
1104 KB |
Output is correct |
11 |
Correct |
3 ms |
2044 KB |
Output is correct |
12 |
Correct |
1 ms |
848 KB |
Output is correct |
13 |
Correct |
52 ms |
73032 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
1104 KB |
Output is correct |
18 |
Correct |
3 ms |
1872 KB |
Output is correct |
19 |
Correct |
52 ms |
73128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
10 ms |
10576 KB |
Output is correct |
8 |
Correct |
1 ms |
1020 KB |
Output is correct |
9 |
Correct |
2 ms |
1016 KB |
Output is correct |
10 |
Correct |
2 ms |
1104 KB |
Output is correct |
11 |
Correct |
3 ms |
2044 KB |
Output is correct |
12 |
Correct |
1 ms |
848 KB |
Output is correct |
13 |
Correct |
52 ms |
73032 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
1104 KB |
Output is correct |
18 |
Correct |
3 ms |
1872 KB |
Output is correct |
19 |
Correct |
52 ms |
73128 KB |
Output is correct |
20 |
Correct |
9 ms |
5724 KB |
Output is correct |
21 |
Correct |
5 ms |
5456 KB |
Output is correct |
22 |
Correct |
3 ms |
848 KB |
Output is correct |
23 |
Correct |
22 ms |
24656 KB |
Output is correct |
24 |
Correct |
15 ms |
7248 KB |
Output is correct |
25 |
Correct |
198 ms |
241480 KB |
Output is correct |