Problem p00876 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; ++i)
#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define P pair<int, C>
#define d first
#define c second

class C {
public:
  int time, cnt, now;
  C() { now = 0; }
  bool operator<(const C &c) const { return time < c.time; }
};

int main() {
  int n;
  while (cin >> n && n) {
    vector<P> v(n);
    REP(i, n) {
      cin >> v[i].c.time >> v[i].c.cnt;
      v[i].d = v[i].c.time;
    }
    sort(v.begin(), v.end());

    int ans = 0;

    // debug
    // int cnt = 0;

    while (v.size() != 0) {
      // while(!isEnd(v) && cnt < 670){

      // debug
      // cnt += 1;

      if (v[0].c.cnt == 0)
        continue;

      ans += v[0].d;
      FOR(i, 1, v.size()) if (v[i].c.cnt != 0) v[i].d -= v[0].d;
      int next = (v[0].c.now + 1) % 2;
      v[0].d = v[0].c.time;
      FOR(i, 1, v.size()) {
        if (v[i].c.cnt == 0 || v[i].c.now != next)
          continue;

        // debug
        // cout <<"@ " <<v[0].d <<", " <<v[i].d <<", "<<(v[0].d < v[i].d)
        // <<endl;

        // if(v[0].d < v[i].d) v[0].d = max(v[0].d, v[i].d);
        v[0].d = max(v[0].d, v[i].d);
      }
      v[0].c.now = next;
      if (next == 0) {
        v[0].c.cnt -= 1;

        // debug
        // cout <<"end? " <<v[0].c.cnt <<endl;

        if (v[0].c.cnt == 0)
          v.erase(v.begin());
      }

      sort(v.begin(), v.end());

      // debug
      // REP(i, v.size()){
      //   cout <<i <<", d = " <<v[i].d <<", c.now = " <<v[i].c.now <<", c.cnt =
      //   " <<v[i].c.cnt <<endl;
      // }
      // cout <<"------" <<endl;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; ++i)
#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define P pair<int, C>
#define d first
#define c second

class C {
public:
  int time, cnt, now;
  C() { now = 0; }
  bool operator<(const C &c) const { return time < c.time; }
};

bool isEnd(vector<P> &v) {
  REP(i, v.size()) if (v[i].c.cnt != 0) return false;
  return true;
}

int main() {
  int n;
  while (cin >> n && n) {
    vector<P> v(n);
    REP(i, n) {
      cin >> v[i].c.time >> v[i].c.cnt;
      v[i].d = v[i].c.time;
    }
    sort(v.begin(), v.end());

    int ans = 0;
    while (!isEnd(v)) {
      if (v[0].c.cnt == 0)
        continue;
      ans += v[0].d;
      FOR(i, 1, v.size()) if (v[i].c.cnt != 0) v[i].d -= v[0].d;
      int next = (v[0].c.now + 1) % 2;
      v[0].d = v[0].c.time;
      FOR(i, 1, v.size()) {
        if (v[i].c.cnt == 0 || v[i].c.now != next)
          continue;
        v[0].d = max(v[0].d, v[i].d);
      }
      v[0].c.now = next;
      if (next == 0)
        v[0].c.cnt -= 1;
      sort(v.begin(), v.end());
    }
    cout << ans << endl;
  }
  return 0;
}