CodeForces - 913C (greedy)

End point dishes, they found seem to feel something was lacking?

Think back home soon an unnamed Lin seniors once again but lost in thought. . . . . . . . .

He intends to point silently to the front desk a few bottles of Erguotou.

He found on the menu n  kinds of different mL of wine.  I  planted with 2 I  -. 1  ml price is C I  . Element stores the number of each type of wine may be considered infinite. .

For much of his own drink or a little B-Tree, but believes he must drink a drink of fun, so he intends to buy at least L ml, but have to spend the least money, and now he wants to know how much money he needs to spend at least

Input

The first line of the input two integers  n  and  L  ( . 1 ≤  n  ≤ 30;  . 1 ≤  L  ≤ 10 . 9 ) - representatives of wine and the total number n needs to buy

The second line of the input  n integers S  C . 1 ,  C 2 , ...,  C n  ( . 1 ≤  C i  ≤ 10 . 9 ) - the i represents the amount of money required wine

Output

An integer output, at least he bought the L ml, spent the least amount of money

Example

Input
4 12
20 30 70 90
Output
150
Input
4 3
10000 1000 100 10
Output
10
Input
4 3
10 100 1000 10000
Output
30
Input
5 787787787
123456789 234567890 345678901 456789012 987654321
Output
44981600785557577

Note

In the first example, you should spend 90 yuan to buy an 8 ml, spend 60 yuan to buy two 2-ml. You will get a total of 12 ml as long as 150 yuan.

In the second example, even if you need only 3 ml, but a 10 yuan to buy some cheap 8 ml.

In the third example, preferably 10 yuan later three of 1 ml.

Solution: 1 according to the first cost-effective ordering, priority to buy the highest price items, buy the maximum.

    Recording the current money spent by the sum, the log file for later use ans greater than a minimum value equal to L l takes. 

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll INF=9223372036854775807-1;
//9223372036854775807     longlong的最大值 
const int N=40;
struct stu{
    ll price;
    ll v;
    bool friend operator < (const stu &x,const stu &y){
        return x.price * y.v <  y.price *x.v;
    }
}arr[N];
int main(){
    int n,need;
    cin>>n>>need;
    for(int i=1;i<=n;i++){
        scanf("%d",&arr[i].price);
        arr[i].v=1<<(i-1);
    }
    sort(arr+1,arr+1+n);
    ll res=need;
    ll l=1;
    ll sum=0;
    ll ans= INF; 
     the while (RES> 0 && L <= n-) { 
        SUM + = RES / ARR [L] .v * ARR [L] .price; // time of maximum purchase RES / ARR [L] .v 
        RES = arr% RES [l] .v; // RES is buying the No. l drink, how much worse from need. 
        IF (RES == 0 ) ANS = min (ANS, SUM); // just buying the then retain it the current state of 
        the else ANS = min (ANS, ARR SUM + [L] .price); // if res = 0, then it is a buy price is more ARR + SUM [L] .price;! 
        L ++ ; 
    } 
    the printf ( " % LLD \ n- " , ANS);
     return  0 ; 
}

 

 

Guess you like

Origin www.cnblogs.com/Accepting/p/11373286.html