Codeforces Round #552 (Div. 3)F. Shovels Shop【DP】

F. Shovels Shop

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn shovels in the nearby shop. The ii-th shovel costs aiai bourles.

Misha has to buy exactly kk shovels. Each shovel can be bought no more than once.

Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset.

There are also mm special offers in the shop. The jj-th of them is given as a pair (xj,yj)(xj,yj), and it means that if Misha buys exactly xjxjshovels during one purchase then yjyj most cheapest of them are for free (i.e. he will not pay for yjyj most cheapest shovels during the current purchase).

Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers).

Your task is to calculate the minimum cost of buying kk shovels, if Misha buys them optimally.

Input

The first line of the input contains three integers n,mn,m and kk (1≤n,m≤2⋅105,1≤k≤min(n,2000)1≤n,m≤2⋅105,1≤k≤min(n,2000)) — the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105), where aiai is the cost of the ii-th shovel.

The next mm lines contain special offers. The jj-th of them is given as a pair of integers (xi,yi)(xi,yi) (1≤yi≤xi≤n1≤yi≤xi≤n) and means that if Misha buys exactly xixi shovels during some purchase, then he can take yiyi most cheapest of them for free.

Output

Print one integer — the minimum cost of buying kk shovels if Misha buys them optimally.

Examples

input

Copy

7 4 5
2 5 4 2 6 3 1
2 1
6 5
2 1
3 1

output

Copy

7

input

Copy

9 4 8
6 8 5 1 8 1 1 2 1
9 2
8 4
5 3
9 7

output

Copy

17

input

Copy

5 1 4
2 5 7 4 6
5 4

output

Copy

17

Note

In the first example Misha can buy shovels on positions 11 and 44 (both with costs 22) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions 33 and 66 (with costs 44 and 33) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position 77 with cost 11. So the total cost is 4+2+1=74+2+1=7.

In the second example Misha can buy shovels on positions 11, 22, 33, 44 and 88 (costs are 66, 88, 55, 11 and 22) and get three cheapest (with costs 55, 11 and 22) for free. And then he can buy shovels on positions 66, 77 and 99 (all with costs 11) without using any special offers. So the total cost is 6+8+1+1+1=176+8+1+1+1=17.

In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost 1717.

分析:因为优惠只能优惠最便宜的,所以最后买到手的肯定是最便宜的K个,然后K<=2000,就可以直接上DP了。

用DP[i]表示前i个能优惠的最多钱,最后减去即可。两重循环+预处理前缀和枚举一下即可。

#include "bits/stdc++.h"

using namespace std;
int s[200004];
struct node {
    int x, y;
} b[200004];
int a[200004];
int pre[200004];
int dp[2004];

int main() {
    int n, m, k;
    cin >> n >> m >> k;
    pre[0] = 0;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        pre[i] = pre[i - 1] + a[i];
    }
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n; ++i) {
        pre[i] = pre[i - 1] + a[i];
    }
    int x, y;
    int cnt = 0;
    for (int i = 0; i < m; ++i) {
        scanf("%d%d", &x, &y);
        if (x <= k) {
            b[cnt++] = {x, y};
        }
    }
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= k; ++i) {
        for (int j = 0; j < cnt; ++j) {
            if (i >= b[j].x) {
                dp[i] = max(dp[i], dp[i - b[j].x] + pre[min(i - b[j].x + b[j].y, k)] - pre[i - b[j].x]);
            }
        }
        dp[i] = max(dp[i], dp[i - 1]);
    }
    printf("%d\n", pre[k] - dp[k]);
}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/89405119