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;
相關文章
相關標籤/搜索