Skip to content

Instantly share code, notes, and snippets.

@asi1024
Created July 17, 2014 21:29
Show Gist options
  • Save asi1024/9ce97b7d7590122e06db to your computer and use it in GitHub Desktop.
Save asi1024/9ce97b7d7590122e06db to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <algorithm>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
using namespace std;
int main() {
int t, n;
while (cin >> n, n) {
queue<pair<int,pair<int,int>>> q1, q2;
vector<pair<int,int>> dat(n);
REP(i,n) cin >> dat[i].first >> dat[i].second;
sort(ALL(dat));
REP(i,n) q1.push({dat[i].first, dat[i]});
for (t = 0; !q1.empty() || !q2.empty(); t++) {
vector<pair<int,pair<int,int>>> a, b;
while (!q1.empty() && q1.front().first <= t) {
a.push_back(q1.front());
q1.pop();
}
REP(i,a.size()) a[i].first = t + a[i].second.first;
sort(ALL(a));
REP(i,a.size()) q2.push(a[i]);
while (!q2.empty() && q2.front().first <= t) {
b.push_back(q2.front());
q2.pop();
}
REP(i,b.size()) {
b[i].first = t + b[i].second.first;
b[i].second.second--;
}
sort(ALL(b));
REP(i,b.size()) if (b[i].second.second > 0) q1.push(b[i]);
}
cout << t - 1 << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment