dp(买票优惠)

CodeForces - 1154F

There are n shovels in the nearby shop. The i-th shovel costs ai

bourles.

Misha has to buy exactly k

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 m

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

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 k

shovels, if Misha buys them optimally.

Input

The first line of the input contains three integers n,m

and k (1n,m2105,1kmin(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 n

integers a1,a2,,an (1ai2105), where ai is the cost of the i

-th shovel.

The next m

lines contain special offers. The j-th of them is given as a pair of integers (xi,yi) (1yixin) and means that if Misha buys exactly xi shovels during some purchase, then he can take yi

most cheapest of them for free.

Output

Print one integer — the minimum cost of buying k

shovels if Misha buys them optimally.

Examples

Input
7 4 5
2 5 4 2 6 3 1
2 1
6 5
2 1
3 1
Output
7
Input
9 4 8
6 8 5 1 8 1 1 2 1
9 2
8 4
5 3
9 7
Output
17
Input
5 1 4
2 5 7 4 6
5 4
Output
17

Note

In the first example Misha can buy shovels on positions 1

and 4 (both with costs 2) 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 3 and 6 (with costs 4 and 3) 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 7 with cost 1. So the total cost is 4+2+1=7

.

In the second example Misha can buy shovels on positions 1

, 2, 3, 4 and 8 (costs are 6, 8, 5, 1 and 2) and get three cheapest (with costs 5, 1 and 2) for free. And then he can buy shovels on positions 6, 7 and 9 (all with costs 1) without using any special offers. So the total cost is 6+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 17

.

 题意:n张票,m种优惠(买x张票,免最便宜的y张票),买k张票

解法:排序,对前k张票dp。

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
const int N = 1e7 + 5 ;
int a[2000009] , sum[200009];
int x[2000009] , y[200009];
int dp[2000009];

int main()
{
    int n , m , k;
    scanf("%d%d%d" , &n , &m , &k);
    for(int i = 1 ; i <= n ; i++)
    {
        scanf("%d" , &a[i]);
    }
    for(int i = 1 ; i <= m ; i++)
    {
        scanf("%d%d" , &x[i] , &y[i]);
    }
    sort(a+1 , a+n+1);
    for(int i = 1 ; i <= n ; i++)
    {
        sum[i] = sum[i-1]+a[i];
    }
    for(int i = 1 ; i <= k ; i++)
    {
        dp[i] = sum[i];
        for(int j = 1 ; j <= m ; j++)
        {
            if(i >= x[j])
            {
                dp[i] = min(dp[i] , dp[i-x[j]]+sum[i]-sum[i-x[j]+y[j]]);//如果要用该张优惠卷
                //那就从原来票数量中减去x【j】张,加上新的没有免费的票价(减去了免费的票价)。
            }
        }
    }
    cout << dp[k] << endl ;


    return 0 ;
}

猜你喜欢

转载自www.cnblogs.com/nonames/p/11821228.html
今日推荐