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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n;
  while (cin >> n, n) {
    vi hp(n);
    rep(i, n) cin >> hp[i];
    int m;
    cin >> m;
    vp a, b;
    rep(i, m) {
      string c, d;
      int e, f;
      cin >> c >> e >> d >> f;
      if (d == "Single")
        a.pb(pii(e, f));
      if (d == "All")
        b.pb(pii(e, f));
    }
    vi dps(1000000, inf), dpa(1000000, inf);
    dps[0] = dpa[0] = 0;
    rep(i, a.size()) rep(j, 1000000) {
      int t = j - a[i].second;
      if (t < 0)
        t = 0;
      dps[j] = min(dps[j], dps[t] + a[i].first);
    }
    rep(i, b.size()) rep(j, 1000000) {
      int t = j - b[i].second;
      if (t < 0)
        t = 0;
      dpa[j] = min(dpa[j], dpa[t] + b[i].first);
    }
    int out = inf;
    rep(i, 1000000) if (dpa[i] != inf) {
      //			cout<<i<<" "<<dpa[i]<<endl;
      int sum = dpa[i];
      bool h = true;
      rep(j, n) {
        int t = hp[j] - i;
        if (t <= 0)
          continue;
        if (dps[t] == inf)
          h = false;
        sum += dps[t];
      }
      if (h)
        out = min(out, sum);
    }
    cout << out << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n;
  while (cin >> n, n) {
    vi hp(n);
    rep(i, n) cin >> hp[i];
    int m;
    cin >> m;
    vp a, b;
    rep(i, m) {
      string c, d;
      int e, f;
      cin >> c >> e >> d >> f;
      if (d == "Single")
        a.pb(pii(e, f));
      if (d == "All")
        b.pb(pii(e, f));
    }
    vi dps(100000, inf), dpa(100000, inf);
    dps[0] = dpa[0] = 0;
    rep(i, a.size()) rep(j, 100000) {
      int t = j - a[i].second;
      if (t < 0)
        t = 0;
      dps[j] = min(dps[j], dps[t] + a[i].first);
    }
    rep(i, b.size()) rep(j, 100000) {
      int t = j - b[i].second;
      if (t < 0)
        t = 0;
      dpa[j] = min(dpa[j], dpa[t] + b[i].first);
    }
    int out = inf;
    rep(i, 100000) if (dpa[i] != inf) {
      int sum = dpa[i];
      rep(j, n) {
        int t = hp[j] - i;
        if (t <= 0)
          continue;
        while (dps[t] == inf)
          t++;
        sum += dps[t];
      }
      out = min(out, sum);
    }
    cout << out << endl;
  }
}