POJ 1276 Cash Machine

做一个水题,回去歇着了……

/*
POJ: 1276 Cash Machine
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>

using namespace std;

struct Case {
    int n, d;
} cash[15];

bool deliver[100005];
int C, N;

int main()
{
    while(scanf("%d%d", &C, &N) != EOF) {
        for(int i = 0; i < N; i++)
            scanf("%d%d", &cash[i].n, &cash[i].d);
        
        memset(deliver, false, sizeof(deliver));
        deliver[0] = true;
        for(int i = 0; i < N; i++) {
            for(int j = C; j >= 0; j--) {
                if(deliver[j]) {
                    for(int k = 1; k <= cash[i].n; k++) {
                        if(j + k * cash[i].d <= C)
                            deliver[j + k * cash[i].d] = true;
                        else
                            break;
                    }
                }
            }
        }
        
        for(int i = C; i >= 0; i--) {
            if(deliver[i]) {
                printf("%d\n", i);
                break;
            }
        }
    }
    
    return 0;
}


你可能感兴趣的:(POJ 1276 Cash Machine)