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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

int main() {
  int w, d;
  while (cin >> w >> d, w) {
    vi front(w), side(d);
    REP(i, w) { cin >> front[i]; }
    REP(i, d) { cin >> side[i]; }
    sort(RALL(front));
    sort(RALL(side));

    int ans = 0;
    int fpos = 0, spos = 0;
    while (fpos < w || spos < d) {
      if (fpos >= w) {
        ans += side[spos];
        spos++;
      } else if (spos >= d) {
        ans += front[fpos];
        fpos++;
      } else if (front[fpos] == side[spos]) {
        ans += front[fpos];
        fpos++;
        spos++;
      } else if (front[fpos] > side[spos]) {
        ans += front[fpos];
        fpos++;
      } else if (front[fpos] < side[spos]) {
        ans += side[spos];
        spos++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

int main() {
  int w, d;
  while (cin >> w >> d, w) {
    vi front(w), side(w);
    REP(i, w) { cin >> front[i]; }
    REP(i, w) { cin >> side[i]; }
    sort(RALL(front));
    sort(RALL(side));

    int ans = 0;
    int fpos = 0, spos = 0;
    while (fpos < w && spos < d) {
      if (fpos >= w) {
        ans += side[spos];
      } else if (spos >= d) {
        ans += front[fpos];
      } else if (front[fpos] == side[spos]) {
        ans += front[fpos];
      } else {
        ans += (front[fpos] + side[spos]);
      }
      fpos++;
      spos++;
    }
    cout << ans << endl;
  }

  return 0;
}