Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 55;
- const long long INF = 1e18;
- int n, m, q;
- int a[N], b[N], p[N];
- int x[N], y[N];
- long long f[N][N][N][N];
- long long solve(int t, int l, int r, int lim) {
- long long &ret = f[t][l][r][lim];
- if (ret != -1) return ret;
- if (l > r) return ret = 0;
- ret = INF;
- int p = 0;
- for (int i = l; i <= r; ++i) {
- if (b[i] >= lim) continue;
- if (!p || b[i] > b[p]) p = i;
- }
- if (t) {
- ret = min(ret, solve(t - 1, l, r, b[p]));
- }
- if (!p) return ret = 0;
- long long cost = 0;
- for (int i = 1; i <= q; ++i) {
- if (x[i] >= l && y[i] <= r && x[i] <= p && p <= y[i]) cost += a[p];
- }
- for (int i = 0; i <= t; ++i) {
- ret = min(ret, solve(i, l, p - 1, b[p]) + solve(t - i, p + 1, r, b[p]) + cost);
- }
- return ret;
- }
- int main() {
- ios::sync_with_stdio(false);
- cin >> n >> m >> q;
- for (int i = 1; i <= n; ++i) cin >> a[i], p[i] = i;
- sort(p + 1, p + 1 + n, [&] (int x, int y) {
- return a[x] < a[y];
- });
- for (int i = 1; i <= n; ++i) b[p[i]] = i;
- for (int i = 1; i <= q; ++i) {
- cin >> x[i] >> y[i];
- }
- memset(f, -1, sizeof f);
- cout << solve(m, 1, n, n + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement