alexjercan/bugnet huggingface dataset:
Problem p01144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
using namespace std;

int main() {
  while (true) {
    int n, m;
    int d, p;
    multiset<pair<int, int>, greater<pair<int, int>>> s;
    multiset<pair<int, int>, greater<pair<int, int>>>::iterator it;
    int e = 0;
    cin >> n >> m;
    if (!(n || m))
      break;
    for (int i = 0; i < n; ++i) {
      cin >> d >> p;
      s.insert(make_pair(p, d));
    }
    it = s.begin();
    while (it != s.end() && m > 0) {
      m -= (*it).second;
      ++it;
    }
    if (m < 0) {
      --it;
      e += (*it).first * (-m);
      ++it;
    }
    while (it != s.end()) {
      e += (*it).second * (*it).first;
      ++it;
    }
    cout << e << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
using namespace std;

int main() {
  while (true) {
    int n, m;
    int d, p;
    multiset<pair<int, int>, greater<pair<int, int>>> s;
    multiset<pair<int, int>, greater<pair<int, int>>>::iterator it;
    int e = 0;
    cin >> n >> m;
    if (!(n || m))
      break;
    for (int i = 0; i < n; ++i) {
      cin >> d >> p;
      s.insert(make_pair(p, d));
    }
    it = s.begin();
    while (it != s.end() || m > 0) {
      m -= (*it).second;
      ++it;
    }
    if (m < 0) {
      --it;
      e += (*it).first * (-m);
      ++it;
    }
    while (it != s.end()) {
      e += (*it).second * (*it).first;
      ++it;
    }
    cout << e << endl;
  }
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1000000;
const int mod = 1e9 + 7;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    vector<pii> v(n);
    rep(i, n) cin >> v[i].second >> v[i].first;
    sort(all(v));
    reverse(all(v));
    int ans = 0, d;
    rep(i, n) {
      d = min(m, v[i].second);
      m -= d;
      v[i].second -= d;
      ans += v[i].first * v[i].second;
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n, m;
pair<int, int> p[10000];

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      int d, pi;
      cin >> d >> pi;
      p[i] = make_pair(pi, d);
    }
    sort(p, p + n);
    reverse(p, p + n);
    int i = 0;
    while (1) {
      if (m <= 0)
        break;
      if (p[i].second <= m) {
        m -= p[i].second;
        p[i].second = 0;
        i++;
      } else {
        p[i].second -= m;
        m = 0;
      }
    }
    int ans = 0;
    for (int j = 0; j < n; j++) {
      ans += p[j].first * p[j].second;
    }
    cout << ans << endl;
  }
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

vector<pii> ans;

int main() {
  while (1) {

    int N, M;
    cin >> N >> M;
    if (!N)
      break;
    ans.clear();
    for (int i = 0; i < N; i++) {
      int d, p;
      cin >> d >> p;
      ans.push_back(pii(p, d));
    }
    sort(ans.begin(), ans.end());
    long long sum = 0;
    while (M > 0 and ans.size()) {
      int id = ans.size() - 1;
      if (M > ans[id].second) {
        M -= ans[id].second;
        ans.pop_back();
      } else if (M > 0) {
        ans[id].second -= M;
        M = 0;
      }
    }

    for (auto x : ans) {
      sum += x.first * x.second;
    }
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

vector<pii> ans;

int main() {
  while (1) {

    int N, M;
    cin >> N >> M;
    if (!N)
      break;
    ans.clear();
    for (int i = 0; i < N; i++) {
      int d, p;
      cin >> d >> p;
      ans.push_back(pii(p, d));
    }
    sort(ans.begin(), ans.end());
    long long sum = 0;
    while (M > 0) {
      int id = ans.size() - 1;
      if (M > ans[id].second) {
        M -= ans[id].second;
        ans.pop_back();
      } else if (M > 0) {
        ans[id].second -= M;
        M = 0;
      }
    }

    for (auto x : ans) {
      sum += x.first * x.second;
    }
    cout << sum << endl;
  }
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef long long L;
typedef pair<L, L> P;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    vector<P> v;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      long long a, b;
      cin >> a >> b;
      v.push_back(make_pair(a, b));
      sum += a * b;
    }

    L p[11] = {};
    for (int i = 0; i < n; i++) {
      p[v[i].second] += v[i].first;
    }

    for (int i = 10; i > -1; i--) {
      if (0 < m) {
        if (m >= p[i])
          sum -= p[i] * i;
        else
          sum -= m * i;

        m -= p[i];
      }
    }

    if (sum < 0)
      sum = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;

int main() {
  int n, m;
  while (cin >> n >> m, n, m) {
    vector<P> v;
    int sum = 0;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      v.push_back(make_pair(b, a));
      sum += a * b;
    }

    sort(v.begin(), v.end());
    while (m > 0) {
      if (v[v.size() - 1].second <= m) {
        sum -= v[v.size() - 1].second * v[v.size() - 1].first;
        m -= v[v.size() - 1].second;
        v.pop_back();
      } else {
        sum -= v[v.size() - 1].first * m;
        m = 0;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;

int main() {
  for (int tci = 0;; tci++) {
    int N, M;
    scanf("%d%d", &N, &M);
    if (!N)
      break;
    pair<int, int> *pd = new pair<int, int>[N];
    int ept = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d%d", &pd[i].second, &pd[i].first);
      ept += pd[i].second * pd[i].first;
    }
    sort(pd, pd + N);
    reverse(pd, pd + N);
    int x = M;
    for (int i = 0; i < N && x > 0; i++) {
      ept -= min(pd[i].second, x) * pd[i].first;
      x -= pd[i].second;
    }
    printf("%d\\n", ept);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  for (int N, M, P; cin >> N >> M >> P, N;) {
    int bet = 0;
    vector<int> l;
    for (int i = 0; i < N; i++) {
      int X;
      cin >> X;
      l.push_back(X);
      bet += X;
    }
    if (l[M - 1]) {
      cout << (bet * (100 - P) / l[M - 1]) << endl;
    } else {
      cout << 0 << endl;
    }
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int m, n;
  n = 1;
  while (n > 0) {
    cin >> n >> m;
    if (n == 0)
      break;

    int d[n], p[n];
    int s = 0;
    for (int i = 0; i < n; i++) {
      cin >> d[i] >> p[i];
      s += d[i] * p[i];
    }

    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        int a, b;
        if (p[j] < p[i]) {
          a = d[i];
          b = p[i];
          d[i] = d[j];
          p[i] = p[j];
          d[j] = a;
          p[j] = b;
        }
      }
    }

    int i = 0;
    while (m > 0 && i <= n - 1) {
      if (m >= d[n - i - 1]) {
        m -= d[n - i - 1];
        s -= d[n - i - 1] * p[n - i - 1];
      } else {
        s -= m * p[n - i - 1];
        m = 0;
      }
      i++;
    }
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int m, n;
  m = 1;
  n = 1;
  while (n > 0) {
    cin >> m >> n;
    int d[n], p[n];
    int s = 0;
    for (int i = 0; i < n; i++) {
      cin >> d[i] >> p[i];
      s += d[i] * p[i];
    }

    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        int a, b;
        if (p[j] < p[i]) {
          a = d[i];
          b = p[i];
          d[i] = d[j];
          p[i] = p[j];
          d[j] = a;
          p[j] = b;
        }
      }
    }

    int i = 0;
    while (m > 0 && i <= n - 1) {
      if (m >= d[n - i - 1]) {
        m -= d[n - i - 1];
        s -= d[n - i - 1] * p[n - i - 1];
      } else {
        m = 0;
        s -= d[n - i - 1] * p[n - i - 1];
      }
      i++;
    }
    if (m != 0 && n != 0) {
      cout << s << endl;
    }
  }
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
  int N, M;
  cin >> N;
  cin >> M;

  while (N != 0 || M != 0) {
    std::vector<std::vector<int>> all(11);
    int money = M;
    int ex_all = 0;
    int cost;
    int d, e;

    for (int i = 0; i < N; i++) {
      cin >> d;
      cin >> e;
      std::vector<int>::iterator itr = all[e].begin();
      itr = all[e].insert(itr, d);
    }

    for (int i = 10; i > 0 && money > 0; i--) {
      for (std::vector<int>::iterator itr = all[i].begin(); itr != all[i].end();
           ++itr) {
        if (*itr < money) {
          money -= *itr;
          *itr = 0;
        } else {
          *itr -= money;
          money = 0;
        }
      }
    }
    for (int i = 1; i < 11; i++) {
      for (std::vector<int>::iterator itr = all[i].begin(); itr != all[i].end();
           ++itr) {
        ex_all += i * (*itr);
      }
    }
    cout << ex_all << endl;
    cin >> N;
    cin >> M;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

struct data_t {
  int dis;
  int ex;

  bool operator<(const data_t &right) const {
    return (ex == right.ex) || (ex > right.ex);
  }
};

int main(void) {
  int N, M;
  cin >> N;
  cin >> M;

  while (N != 0 || M != 0) {
    std::vector<data_t> data(N);
    int money = M;
    int ex_all = 0;
    int cost;
    for (int i = 0; i < N; i++) {
      cin >> data[i].dis;
      cin >> data[i].ex;
      ex_all += data[i].dis * data[i].ex;
    }
    std::sort(data.begin(), data.end());
    for (int i = 0; i < N && money > 0; i++) {
      cost = data[i].dis * data[i].ex;
      if (data[i].dis < money) {
        ex_all -= cost;
        money -= data[i].dis;
      } else {
        ex_all -= money * data[i].ex;
        money = 0;
      }
    }
    cout << ex_all << endl;

    cin >> N;
    cin >> M;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;

int main() {
  for (int tci = 0;; tci++) {
    int N, M;
    scanf("%d%d", &N, &M);
    if (!N)
      break;
    pair<int, int> *pd = new pair<int, int>[N];
    int ept = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d%d", &pd[i].second, &pd[i].first);
      ept += pd[i].second * pd[i].first;
    }
    sort(pd, pd + N);
    reverse(pd, pd + N);
    int x = M;
    for (int i = 0; i < N && x > 0; i++) {
      ept -= min(pd[i].second, x) * pd[i].first;
      x -= pd[i].second;
    }
    printf("%d\\n", ept);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int main() {

  int N, M;

  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;

    vector<pair<int, int>> PD(N);

    int f, s;
    for (int i = 0; i < N; i++) {
      cin >> PD[i].second >> PD[i].first;
    }

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

    int k = N - 1;
    while (M > 0 || k >= 0) {

      if (PD[k].second <= M)
        PD[k].second = 0;
      else
        PD[k].second -= M;

      M -= PD[k].second;
      k--;
    }

    int prob = 0;

    for (int i = 0; i < N; i++)
      prob += PD[i].first * PD[i].second;

    cout << prob << endl;
  }

  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;

typedef pair<int, int> P;

int main() {
  int n;
  long long m;
  while (cin >> n >> m && (n || m)) {
    priority_queue<P> que;
    int d, p;
    for (int i = 0; i < n; ++i) {
      cin >> d >> p;
      que.push(P(p, d));
    }
    long long ans = 0;
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      m -= p.second;
      if (m <= 0) {
        ans += p.first * abs(m);
        break;
      }
    }
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      ans += p.first * p.second;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;

typedef pair<int, int> P;

int main() {
  int n;
  long long m;
  while (cin >> n >> m && (n || m)) {
    priority_queue<P> que;
    int d, p;
    for (int i = 0; i < n; ++i) {
      cin >> d >> p;
      que.push(P(p, d));
    }
    long long ans = 0;
    while (1) {
      P p = que.top();
      que.pop();
      m -= p.second;
      if (m <= 0) {
        ans += p.first * abs(m);
        break;
      }
    }
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      ans += p.first * p.second;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01144 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
  int n, m, d, p, res, cnt[11];
  while (cin >> n >> m, n | m) {
    res = 0;
    for (int i = 0; i < 11; ++i)
      cnt[i] = 0;
    for (int i = 0; i < n; ++i) {
      cin >> d >> p;
      cnt[p] += d;
    }
    for (int i = 10; i > 0; --i) {
      if (m > cnt[i]) {
        m -= cnt[i];
      } else {
        res += i * (cnt[i] - m);
        m = 0;
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
  int n, m, d, p;
  vector<int> exp;
  while (cin >> n >> m, n | m) {
    exp.clear();
    for (int i = 0; i < n; ++i) {
      cin >> d >> p;
      for (int j = 0; j < d; ++j)
        exp.push_back(p);
    }
    if (exp.size() <= m) {
      cout << 0 << endl;
      continue;
    }
    sort(exp.begin(), exp.end(), greater<int>());
    cout << accumulate(exp.begin() + m, exp.end(), 0) << endl;
  }
  return 0;
}
Problem p01146 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll inf = 1e9;

int main() {
  ios::sync_with_stdio(false);
  int n, m, l, k, a, h;
  while (cin >> n >> m >> l >> k >> a >> h, m) {
    vector<ll> cap{a, h};
    vector<vector<ll>> dist(n, vector<ll>(n, inf));
    for (int i = 0; i < l; ++i) {
      int ca;
      cin >> ca;
      cap.push_back(ca);
    }
    for (int i = 0; i < k; ++i) {
      int u, v, c;
      cin >> u >> v >> c;
      dist[u][v] = c;
      dist[v][u] = c;
    }
    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          if (dist[i][j] > dist[i][k] + dist[k][j]) {
            dist[i][j] = dist[i][k] + dist[k][j];
          }
        }
      }
    }
    vector<vector<ll>> d(l + 2, vector<ll>(l + 2, inf));
    for (int i = 0; i < cap.size(); ++i) {
      for (int j = 0; j < cap.size(); ++j) {
        d[i][j] = dist[cap[i]][cap[j]];
        if (d[i][j] > m)
          d[i][j] = inf;
      }
    }
    for (int k = 0; k < l + 2; ++k) {
      for (int i = 0; i < l + 2; ++i) {
        for (int j = 0; j < l + 2; ++j) {
          if (d[i][j] > d[i][k] + d[k][j]) {
            d[i][j] = d[i][k] + d[k][j];
          }
        }
      }
    }
    if (d[0][1] >= inf)
      cout << "Help!" << endl;
    else {
      ll ans = max((ll)0, d[0][1] - m);
      cout << ans + d[0][1] << endl;
    }
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll inf = 1e9;

int main() {
  ios::sync_with_stdio(false);
  int n, m, l, k, a, h;
  while (cin >> n >> m >> l >> k >> a >> h) {
    vector<ll> cap;
    vector<vector<ll>> dist(n, vector<ll>(n, inf));
    cap.push_back(a);
    cap.push_back(h);
    for (int i = 0; i < l; ++i) {
      int ca;
      cin >> ca;
      cap.push_back(ca);
    }
    for (int i = 0; i < k; ++i) {
      int u, v, c;
      cin >> u >> v >> c;
      dist[u][v] = c;
      dist[v][u] = c;
    }
    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          if (dist[i][j] > dist[i][k] + dist[k][j]) {
            dist[i][j] = dist[i][k] + dist[k][j];
          }
        }
      }
    }
    vector<vector<ll>> d(l + 2, vector<ll>(l + 2, inf));
    for (int i = 0; i < cap.size(); ++i) {
      for (int j = 0; j < cap.size(); ++j) {
        d[i][j] = dist[cap[i]][cap[j]];
        if (d[i][j] > m)
          d[i][j] = inf;
      }
    }
    for (int k = 0; k < l + 2; ++k) {
      for (int i = 0; i < l + 2; ++i) {
        for (int j = 0; j < l + 2; ++j) {
          if (d[i][j] > d[i][k] + d[k][j]) {
            d[i][j] = d[i][k] + d[k][j];
          }
        }
      }
    }
    if (d[0][1] >= inf)
      cout << "Help!" << endl;
    else {
      int ans = max((ll)0, d[0][1] - m);
      cout << ans + d[0][1] << endl;
    }
  }
}
Problem p01146 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int, int> P;
typedef pair<int, P> Pi;

struct Edge {
  int to, cost;
};

int N, M, L, K, A, H;
vector<Edge> G[101];
bool is_freeze[101];

int dij() {
  priority_queue<Pi, vector<Pi>, greater<Pi>> Q;
  bool used[101][101] = {{0}};
  Q.push(Pi(0, P(M, A)));
  while (!Q.empty()) {
    Pi p = Q.top();
    Q.pop();
    int c = p.first, m = p.second.first, v = p.second.second;

    if (v == H) {
      return c;
    }

    if (used[v][m])
      continue;
    used[v][m] = true;

    if (is_freeze[v]) {
      if (m < 0)
        Q.push(Pi(c - m, P(0, v)));
      else if (m + 1 <= M)
        Q.push(Pi(c + 1, P(m + 1, v)));
    }

    for (Edge e : G[v]) {
      int to = e.to, cost = e.cost;
      int nm = m - cost;
      if (nm >= 0)
        Q.push(Pi(c + cost, P(nm, to)));
    }
  }

  return -1;
}

signed main() {
  while (cin >> N >> M >> L >> K >> A >> H, N) {
    fill_n(is_freeze, 101, 0);
    for (int i = 0; i < N; i++)
      G[i].clear();
    for (int i = 0; i < L; i++) {
      int a;
      cin >> a;
      is_freeze[a] = true;
    }

    for (int i = 0; i < K; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      G[a].push_back(Edge{b, c});
      G[b].push_back(Edge{a, c});
    }

    int ans = dij();
    if (ans == -1)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int, int> P;
typedef pair<int, P> Pi;

struct Edge {
  int to, cost;
};

int N, M, L, K, A, H;
vector<Edge> G[101];
bool is_freeze[101];

int dij() {
  priority_queue<Pi, vector<Pi>, greater<Pi>> Q;
  int used[101] = {0};
  Q.push(Pi(0, P(M, A)));
  while (!Q.empty()) {
    Pi p = Q.top();
    Q.pop();
    int c = p.first, m = p.second.first, v = p.second.second;

    if (v == H) {
      return c;
    }

    if (used[v] && used[v] > c)
      continue;
    used[v] = m;

    if (is_freeze[v]) {
      if (m < 0)
        Q.push(Pi(c - m, P(0, v)));
      else if (m + 1 <= M)
        Q.push(Pi(c + 1, P(m + 1, v)));
    }

    for (Edge e : G[v]) {
      int to = e.to, cost = e.cost;
      int nm = m - cost;
      if (nm >= 0)
        Q.push(Pi(c + cost, P(nm, to)));
    }
  }

  return -1;
}

signed main() {
  while (cin >> N >> M >> L >> K >> A >> H, N) {
    fill_n(is_freeze, 101, 0);
    for (int i = 0; i < N; i++)
      G[i].clear();
    for (int i = 0; i < L; i++) {
      int a;
      cin >> a;
      is_freeze[a] = true;
    }

    for (int i = 0; i < K; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      G[a].push_back(Edge{b, c});
      G[b].push_back(Edge{a, c});
    }

    int ans = dij();
    if (ans == -1)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}
Problem p01146 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>

using namespace std;

int main() {
  for (int N, M, L, K, A, H;
       cin >> N >> M >> L >> K >> A >> H, N | M | L | K | A | H;) {
    bool freez[100];
    int time[100][100];

    const int INF = 900100100 / 4;

    for (int i = 0; i < N; ++i) {
      freez[i] = false;

      for (int j = 0; j < N; ++j)
        time[i][j] = INF;
    }

    for (int i = 0; i < L; ++i) {
      int x;
      cin >> x;
      freez[x] = true;
    }

    freez[H] = freez[A] = true;

    for (int i = 0; i < K; ++i) {
      int x, y, t;
      cin >> x >> y >> t;
      time[x][y] = time[y][x] = t;
    }

    for (int i = 0; i < N; ++i)
      for (int j = 0; j < N; ++j)
        for (int k = 0; k < N; ++k)
          time[j][k] = min(time[j][i] + time[i][k], time[j][k]);

    for (int i = 0; i < N; ++i)
      for (int j = 0; j < N; ++j)
        if (!freez[i] || !freez[j] || time[i][j] > M)
          time[i][j] = INF;

    for (int i = 0; i < N; ++i)
      for (int j = 0; j < N; ++j)
        for (int k = 0; k < N; ++k)
          time[j][k] = min(time[j][i] + time[i][k], time[j][k]);

    if (time[A][H] == INF)
      cout << "Help!" << endl;
    else
      cout << time[A][H] + max(0, time[A][H] - M) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  for (int N, M, L, K, A, H;
       cin >> N >> M >> L >> K >> A >> H, N | M | L | K | A | H;) {
    bool freez[100];
    bool visit[100][101];
    int time[100][101];
    int length[100][100];

    const int INF = 100100100;

    for (int i = 0; i < N; ++i) {
      freez[i] = false;

      for (int j = 0; j < 101; ++j) {
        visit[i][j] = false;
        time[i][j] = INF;
      }

      for (int j = 0; j < N; ++j)
        length[i][j] = INF;
    }

    for (int i = 0; i < L; ++i) {
      int x;
      cin >> x;
      freez[x] = true;
    }

    for (int i = 0; i < K; ++i) {
      int x, y, t;
      cin >> x >> y >> t;
      length[x][y] = length[y][x] = t;
    }

    time[A][M] = 0;

    int ans = INF;

    while (true) {
      int t = INF;
      int m;
      int cur;

      for (int i = 0; i < N; ++i)
        for (int j = 0; j < 101; ++j)
          if (visit[i][j] == false && time[i][j] < t) {
            cur = i;
            t = time[i][j];
            m = j;
          }

      if (t == INF)
        break;

      if (cur == H) {
        ans = t;
        break;
      }

      visit[cur][m] = true;

      for (int i = 0; i < N; ++i)
        if (i != cur)
          for (int j = 0; j < 101; ++j) {
            if (length[cur][i] > m || visit[i][j] || i == j)
              continue;

            int tt = t + length[cur][i];
            int mm = m - length[cur][i];

            time[i][mm] = min(time[i][mm], tt);

            if (freez[i])
              for (int k = 1; k <= M - mm; ++k)
                time[i][mm + k] = min(time[i][mm + k], tt + k);
          }
    }

    if (ans == INF)
      cout << "Help!" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}