#include <iostream> class outJob { public: int ID = -1; int planeNum = 0; bool working = 1; outJob() {} outJob(int id, int NUM) : ID(id), planeNum(NUM), working(1) {} };
class jobQueue { public: outJob data[30000]; int head = 0; int tail = 0;
void enqueue(outJob x) { data[tail++] = x; } void dequeue() { head++; } bool isEmpty() { return (tail == head); } outJob* queuehead() { return &data[head]; }
void switchJob(int id) { for (int i = head; i < tail; i++) if (id == data[i].ID) data[i].working = 0; }
int clearJob() { int tmp = 0, i; for (i = head; i < tail; i++) { if (data[i].working) { head = i; return tmp; } tmp += data[i].planeNum; } head = i; return tmp; } };
int c, n, okplanes; jobQueue waitingList, outJobList;
int tryOut(outJob x) { int id = x.ID, num = x.planeNum;
int newOut = 0;
if (num <= c) { if (num <= okplanes) { okplanes -= num, newOut = num; outJobList.enqueue(x); } } else { if (okplanes == c) { c = num, okplanes = 0, newOut = num; outJobList.enqueue(x); } } return newOut; }
int main() { while (~scanf("%d %d", &c, &n)) { okplanes = c; for (int i = 0; i < n; ++i) { int newOut = 0; int a, b; scanf("%d %d", &a, &b); outJob newJob(a, b);
if (a >= 0) { newOut = tryOut(newJob); if (!newOut) waitingList.enqueue(newJob); } else { outJobList.switchJob(b); okplanes += outJobList.clearJob(); while (1) { if (waitingList.isEmpty()) break; int tp = tryOut(*waitingList.queuehead()); if (tp == 0) break; newOut += tp, waitingList.dequeue(); } }
printf("%d\n", newOut); } } }
|