USACO milk__已解决

/*
ID: nenusb1
LANG: C
TASK: milk
*/
#include <stdio.h>
#include <string.h>

int main(){
   freopen("milk.in","r",stdin);
   freopen("milk.out","w",stdout);
    int N, M;
    scanf("%d %d", &N, &M);
    int i;
    
    int needs = N;
    int amount[1010];
    memset(amount,0,sizeof(amount));

    for(i=0; i<M; i++){
          int P,A;
          scanf("%d %d",&P, &A);
          //会有相同价格
          //amount[P] = A; 
          amount[P] += A;
    }

    
    int total = 0;    
    i=0;

    while(needs > 0){
        if(amount[i]>0 && amount[i] <= needs)  {
           needs -= amount[i];
           total += i * amount[i];  
                   
        }
        else if(amount[i]>0 && amount[i]> needs){
           total += i * needs;
            break;
        }
           i++; 
   }
    printf("%d\n",total);

    return 0;
}

问题出在:会有相同价格的牛奶,因此

spa

          amount[P] = A;


改为code

          //会有相同价格
          //amount[P] = A; 
          amount[P] += A;
相关文章
相关标签/搜索