#include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,n) for (int i = 0; i < n; i++) #define abs(x) ((x)<0?(-(x)):(x)) #define REP(i,v) for (unsigned i = 0; i < v.size(); i++) #define RL(i,v) for (unsigned i = 0; i < v.length(); i++) typedef long long ll; const int MAX_N = 102; const int MAX_P = 100100; struct Item { int price, quantity; bool operator < (const Item& r) const { return price != r.price ? price > r.price : quantity > r.quantity; } }; int n, space; int a[MAX_N][2]; deque < Item > q; void solve(void) { FILE* f = fopen("summer.in", "rt"); FILE* fo = fopen("summer.out", "wt"); fscanf(f, "%d %d", &n, &space); for (int i = 0; i < n; i++) fscanf(f, "%d %d", &a[i][0], &a[i][1]); int ans = 0; for (int i = 0; i < n; i++) { int rem = a[i][0], price = a[i][1]; while (rem > 0 && !q.empty() && q.front().price < price) { Item cur = q.front(); q.pop_front(); if (cur.quantity <= rem) { ans += cur.quantity * cur.price; rem -= cur.quantity; space += cur.quantity; } else { ans += rem * cur.price; cur.quantity -= rem; space += rem; rem = 0; q.push_front(cur); } } ans += rem * price; while (!q.empty() && q.back().price >= price) { Item cur = q.back(); q.pop_back(); space += cur.quantity; } if (space > 0) { Item add; add.price = price; add.quantity = space; q.push_back(add); space = 0; } } fprintf(fo, "%d\n", ans); fclose(f); fclose(fo); } int main(void) { solve(); return 0; }