2014 03 30 First Post

POSTS

hdu/saving_hdu.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 2014年 3月29日 星期六 08时31分21秒 CST
#include <iostream>
#include <algorithm>
#include <numeric>
#include <bitset>
#include <vector>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

#define fe(i, a, b) for (int i = int(a); i <= int(b); i++)
#define fu(i, a, b) for (int i = int(a); i < int(b); i++)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;
#define pb push_back
#define mp make_pair

struct M {
    int pi, mi;
    M(int p, int m) : pi(p), mi(m) {}
    bool operator<(const M another) const {
        return pi > another.pi;
    }
};

int main()
{
    int v, n, p, m;
    vector<M> vm;
    int value;
    while (true) {
        vm.clear();
        value   = 0;
        cin >> v;
        if (!v) break;
        cin >> n;
        while (n--) {
            cin >> p >> m;
             vm.pb(M(p, m));
        }
        sort(vm.begin(), vm.end());
        for (int i = 0; i < vm.size(); i++) {
            //cout << vm[i].pi << endl;
            if (v >= vm[i].mi) {
                value += vm[i].pi * vm[i].mi;
                v -= vm[i].mi;
            } else {
                value += vm[i].pi * v;
                break;
            }
        }
        cout << value << endl;
    }

    return 0;
}