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

#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  ll n;
  cin >> n;

  ll *d = new ll[n * 2];
  for (ll i = 0; i < n; i++) {
    cin >> d[i];
    if (i != 0) {
      d[i] += d[i - 1];
    }
    d[i + n] = d[i];
  }
  for (ll i = 0; i < n; i++) {
    d[i + n] += d[n - 1];
  }
  for (ll i = 0; i < n * 2; i++) {
    // cout<<i<<"-"<<d[i]<<endl;
  }
  ll max_min = 0;
  ll l = 0, c = 0, r = 0;
  while (l < n) {
    while (d[c] - d[l] < d[n - 1] / 3)
      c++;
    c--;
    for (r = c + 1; r < n + l; r++) {
      ll cr = d[r] - d[c];
      ll rl = d[n + l] - d[r];
      ll m = d[c] - d[l];
      if (m >= cr)
        m = cr;
      if (m >= rl)
        m = rl;
      //	cout<<l<<","<<c<<","<<r<<","<<n+l<<"-"<<d[c]-d[l]<<","<<cr<<","<<rl<<endl;
      if (m > max_min) {
        max_min = m;
      }
    }
    l++;
  }
  cout << max_min << endl;

  delete[] d;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define ULL int64_t

ULL sum(ULL *d, ULL s, ULL e) {
  ULL r = 0;
  for (int i = s; i <= e; i++) {
    r += d[i];
  }
  return r;
}

int main() {
  ULL n;
  cin >> n;
  ULL *d = (ULL *)malloc(sizeof(ULL) * n);
  if (d == NULL) {
    return -1;
  }
  ULL s = 0;
  ULL m;
  for (ULL i = 0; i < n; i++) {
    cin >> m;
    d[i] = m;
    s += m;
  }
  ULL res = 0;
  for (ULL u = 0; u < n - 2; u++) {
    for (ULL v = u + 1; v < n - 1; v++) {
      for (ULL w = v + 1; w < n; w++) {
        ULL snd = sum(d, u, v - 1);
        ULL thd = sum(d, v, w - 1);
        ULL fst = s - snd - thd;
        ULL min;
        if (fst <= snd && fst <= thd) {
          min = fst;
        } else if (snd <= fst && snd <= thd) {
          min = snd;
        } else {
          min = thd;
        }
        if (res < min) {
          res = min;
        }
      }
    }
  }
  cout << res << endl;
  free(d);
}
Problem p00523 (C++) — buggy submission, verdict: Runtime Error

/*
    やるだけでバグって辛い
 */

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

int n;
int a[100000];
int pos[100000];
long long int low;
long long int high;
long long int sum;

bool check(long long int x) {
  int s = 0;
  int t = 0;
  long long int now = 0;

  while (s < n) {
    while ((t + 1) % n != s && now < x) {
      now += a[t];
      t = (t + 1) % n;
    }
    if ((t + 1) % n == s)
      return false;
    pos[s] = t;
    now -= a[s++];
  }

  for (int i = 0; i < n; i++) {
    int len1 = pos[i] - i;
    int len2 = pos[pos[i]] - pos[i];
    int len3 = pos[pos[pos[i]]] - pos[pos[i]];

    if (len1 < 0)
      len1 += n;
    if (len2 < 0)
      len2 += n;
    if (len3 < 0)
      len3 += n;
    if (len1 + len2 + len3 <= n)
      return true;
  }

  return false;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

  low = -1;
  high = (sum / 3) + 1;
  while (high - low > 1) {
    long long int mid = (high + low) / 2;
    if (check(mid))
      low = mid;
    else
      high = mid;
  }

  printf("%lld\\n", low);
}

Error: 0

Fixed submission:

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

int n;
int a[100000];
int pos[100000];
long long int low;
long long int high;
long long int sum;

bool check(long long int x) {
  int s = 0;
  int t = 0;
  long long int now = 0;

  while (s < n) {
    while ((t + 1) % n != s && now < x) {
      now += a[t];
      t = (t + 1) % n;
    }
    assert((t + 1) % n != s);
    pos[s] = t;
    now -= a[s++];
  }

  for (int i = 0; i < n; i++) {
    int len1 = pos[i] - i;
    int len2 = pos[pos[i]] - pos[i];
    int len3 = pos[pos[pos[i]]] - pos[pos[i]];

    if (len1 < 0)
      len1 += n;
    if (len2 < 0)
      len2 += n;
    if (len3 < 0)
      len3 += n;
    if (len1 + len2 + len3 <= n)
      return true;
  }

  return false;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

  low = -1;
  high = (sum / 3) + 1;
  while (high - low > 1) {
    long long int mid = (high + low) / 2;
    if (check(mid))
      low = mid;
    else
      high = mid;
  }

  printf("%lld\\n", low);
}
Problem p00523 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REP(i, a) FOR(i, 0, a)
#define real() X
#define imag() Y
#include <iostream>
using namespace std;
typedef long long ll;
ll sumsum[2000010];
ll sums(int s, int e) {
  if (s > e)
    return 0;
  return sumsum[e + 1] - sumsum[s];
}
ll divide(int ss, int ee) {
  int s = ss;
  int e = ee;
  ll maxim = 0;
  while (s <= e) {
    int f = (s + e) / 2;
    maxim = max(maxim, min(sums(ss, f), sums(f + 1, ee)));
    if (sums(ss, f) <= sums(f + 1, ee))
      s = f + 1;
    else
      e = f - 1;
  }
  return maxim;
}
int main() {
  ll N, p, s, sum = 0;
  vector<ll> baum;
  cin >> N;
  REP(i, N) {
    cin >> p;
    baum.push_back(p);
    sum += p;
    sumsum[i + 1] = sumsum[i] + p;
  }
  REP(i, N) {
    baum.push_back(baum[i]);
    sumsum[N + i + 1] = sumsum[N + i] + baum[i];
  }
  int head = 0;
  ll maxim = 0;
  REP(snake, N) {
    while (sums(snake, head) * 3 < sum)
      head++;
    if (sums(snake + 1, head) * 3 < sum) {
      maxim = max(maxim, divide((head + 1) % N, (snake + N - 1) % N +
                                                    N * ((head + 1) % N >
                                                         (snake + N - 1) % N)));
    }
  }
  cout << maxim << endl;
  // your code goes here
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REP(i, a) FOR(i, 0, a)
#define real() X
#define imag() Y
#include <iostream>
using namespace std;
typedef long long ll;
ll sumsum[200002];
ll sums(int s, int e) {
  if (s > e)
    return 0;
  return sumsum[e + 1] - sumsum[s];
}
ll divide(int ss, int ee) {
  int s = ss;
  int e = ee;
  ll maxim = 0;
  while (s <= e) {
    int f = (s + e) / 2;
    maxim = max(maxim, min(sums(ss, f), sums(f + 1, ee)));
    if (sums(ss, f) <= sums(f + 1, ee))
      s = f + 1;
    else
      e = f - 1;
  }
  return maxim;
}
int main() {
  ll N, p, s, sum = 0;
  vector<ll> baum;
  cin >> N;
  REP(i, N) {
    cin >> p;
    baum.push_back(p);
    sum += p;
    sumsum[i + 1] = sumsum[i] + p;
  }
  REP(i, N) {
    baum.push_back(baum[i]);
    sumsum[N + i + 1] = sumsum[N + i] + baum[i];
  }
  REP(i, N) {
    baum.push_back(baum[i]);
    sumsum[N * 2 + i + 1] = sumsum[N * 2 + i] + baum[i];
  }
  int head = 0;
  ll maxim = 0;
  REP(snake, N) {
    while (sums(snake, head) * 3 < sum)
      head++;
    if (sums(snake + 1, head) * 3 < sum) {
      maxim = max(maxim, divide((head + 1) % N, (snake + N - 1) % N +
                                                    N * ((head + 1) % N >
                                                         (snake + N - 1) % N)));
    }
  }
  cout << maxim << endl;
  // your code goes here
  return 0;
}
Problem p00523 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define Ford(i, a, b) for (int i = (a); i >= (b); i--)

const int maxn = 200000 + 1912;
int n, a[maxn];
long long totalSum = 0;
long long sum[maxn];

void ReadData() {
  scanf("%d", &n);
  For(i, 1, n) scanf("%d", &a[i]);
  For(i, 1, n) a[i + n] = a[i];
  For(i, 1, n << 1) sum[i] = sum[i - 1] + a[i];
}

long long GetSum(const int l, const int r) { return sum[r] - sum[l - 1]; }

bool check(long long limit) {
  int ptr1 = 1, ptr2 = 2;
  For(i, 1, n - 1) {
    int last = i + n - 1;
    while (ptr1 < last - 1 && GetSum(i, ptr1) < limit)
      ptr1++;
    ptr2 = max(ptr2, ptr1 + 1);
    while (ptr2 < last && GetSum(ptr1 + 1, ptr2) < limit)
      ptr2++;

    if ((ptr2 <= last) &&
        (GetSum(i, ptr1) >= limit && GetSum(ptr1 + 1, ptr2) >= limit &&
         GetSum(ptr2 + 1, last) >= limit))
      return true;
  }
  return false;
}

void Process() {
  long long res = 0;
  long long l = 1, r = 1000LL * 1000 * 1000 * 1000 * 1000 + 1912;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid)) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  cout << res << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // freopen("./baumkuchen.inp" , "r" , stdin);
  ReadData();
  Process();

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define Ford(i, a, b) for (int i = (a); i >= (b); i--)

const int maxn = 100000 + 1912;
int n, a[maxn];
long long totalSum = 0;
long long sum[maxn];

void ReadData() {
  scanf("%d", &n);
  For(i, 1, n) scanf("%d", &a[i]);
  For(i, 1, n) a[i + n] = a[i];
  For(i, 1, n << 1) sum[i] = sum[i - 1] + a[i];
}

long long GetSum(const int l, const int r) { return sum[r] - sum[l - 1]; }

bool check(long long limit) {
  int ptr1 = 1, ptr2 = 2;
  For(i, 1, n - 1) {
    int last = i + n - 1;
    while (ptr1 < last - 1 && GetSum(i, ptr1) < limit)
      ptr1++;
    ptr2 = max(ptr2, ptr1 + 1);
    while (ptr2 < last && GetSum(ptr1 + 1, ptr2) < limit)
      ptr2++;

    if ((ptr2 <= last) &&
        (GetSum(i, ptr1) >= limit && GetSum(ptr1 + 1, ptr2) >= limit &&
         GetSum(ptr2 + 1, last) >= limit))
      return true;
  }
  return false;
}

void Process() {
  long long res = 0;
  long long l = 1, r = 1000LL * 1000 * 1000 * 1000 * 1000 + 1912;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid)) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  cout << res << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // freopen("./baumkuchen.inp" , "r" , stdin);
  ReadData();
  Process();

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

#include <algorithm>
#include <stdio.h>

#pragma warning(disable : 4996)

using namespace std;

int n, a[100000];
long long s[200001];

long long sum(int l, int r) { return s[r] - s[l]; }

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

  s[0] = 0;
  for (int i = 1; i <= 2 * n; i++)
    s[i] = s[i - 1] + a[(i - 1) % n];

  long long ret = 0, sums = s[n];

  for (int i = 0; i < n; i++) {
    int posr1 = lower_bound(s, s + 2 * n + 1, s[i] + sums / 3) - s;
    int posl1 = posr1 - 1;
    int posr2 = lower_bound(s, s + 2 * n + 1, s[i] + sums * 2 / 3) - s;
    int posl2 = posr1 - 1;

    ret = max(ret, min({sum(i, posl1), sum(posl1, posl2), sum(posl2, i + n)}));
    ret = max(ret, min({sum(i, posr1), sum(posr1, posl2), sum(posl2, i + n)}));
    ret = max(ret, min({sum(i, posl1), sum(posl1, posr2), sum(posr2, i + n)}));
    ret = max(ret, min({sum(i, posr1), sum(posr1, posr2), sum(posr2, i + n)}));
  }

  printf("%lld\\n", ret);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>

#pragma warning(disable : 4996)

using namespace std;

int n, a[100000];
long long s[200001];

long long sum(int l, int r) { return s[r] - s[l]; }

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

  s[0] = 0;
  for (int i = 1; i <= 2 * n; i++)
    s[i] = s[i - 1] + a[(i - 1) % n];

  long long ret = 0;

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      for (int k = j + 1; k < n; k++) {
        ret = max(ret, min({sum(i, j), sum(j, k), sum(k, n + i)}));
      }
    }
  }

  printf("%lld\\n", ret);

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

#include <bits/stdc++.h>
#define INF (1e17)
#define N 100005
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll n, m, x, h[N];
vector<P> G[N];
priority_queue<P, vector<P>, greater<P>> q;
ll d[N];

ll dijkstra() {

  for (int i = 0; i < n; i++)
    d[i] = INF;
  q.push(P(0, 0));
  d[0] = 0;

  while (!q.empty()) {
    P t = q.top();
    q.pop();
    ll cost = t.first, node = t.second;

    if (node == n - 1)
      return cost + max(cost - x + h[n - 1], 0LL);
    if (d[node] < cost)
      continue;

    for (int i = 0; i < G[node].size(); i++) {
      ll nnode = G[node][i].first;
      ll ncost = cost + G[node][i].second;

      if (x - ncost >= h[nnode])
        ncost += x - ncost - h[nnode];
      if (d[nnode] > ncost) {
        d[nnode] = ncost;
        q.push(P(ncost, nnode));
      }
    }
  }

  return INF;
}

int main() {

  cin >> n >> m >> x;

  for (int i = 0; i < n; i++)
    scanf("%lld", &h[i]);

  ll a, b, c;
  for (int i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &a, &b, &c);
    a--, b--;
    if (h[a] - c >= 0)
      G[a].push_back(P(b, c));
    if (h[b] - c >= 0)
      G[b].push_back(P(a, c));
  }

  ll ans = dijkstra();
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define INF (1e17)
#define N 100005
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll n, m, x, h[N];
vector<P> G[N];
priority_queue<P, vector<P>, greater<P>> q;
ll d[N];

ll dijkstra() {

  for (int i = 0; i < n; i++)
    d[i] = INF;
  q.push(P(0, 0));
  d[0] = 0;

  while (!q.empty()) {
    P t = q.top();
    q.pop();
    ll cost = t.first, node = t.second;

    if (node == n - 1)
      return cost + max(cost - x, 0LL) + h[n - 1];
    if (d[node] < cost)
      continue;

    for (int i = 0; i < G[node].size(); i++) {
      ll nnode = G[node][i].first;
      ll ncost = cost + G[node][i].second;

      if (x - ncost >= h[nnode])
        ncost += x - ncost - h[nnode];

      q.push(P(ncost, nnode));
    }
  }

  return INF;
}

int main() {

  cin >> n >> m >> x;

  for (int i = 0; i < n; i++)
    scanf("%lld", &h[i]);

  ll a, b, c;
  for (int i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &a, &b, &c);
    a--, b--;
    if (h[a] - c >= 0)
      G[a].push_back(P(b, c));
    if (h[b] - c >= 0)
      G[b].push_back(P(a, c));
  }

  ll ans = dijkstra();
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

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

#include <bits/stdc++.h>
#define INF (1e17)
#define N 100005
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll n, m, x, h[N];
vector<P> G[N];
priority_queue<P, vector<P>, greater<P>> q;

ll dijkstra() {
  bool used[N] = {};
  q.push(P(0, 0));
  while (!q.empty()) {
    P temp = q.top();
    q.pop();
    ll cost = temp.first;
    ll node = temp.second;
    if (node == n - 1)
      return cost + max(cost - x + h[node], 0LL);
    if (used[node])
      continue;
    used[node] = 1;
    for (int i = 0; i < G[node].size(); i++) {
      ll nnode = G[node][i].first;
      ll ncost = G[node][i].second + cost;
      ncost += max(x - ncost - h[nnode], 0LL);
      q.push(P(ncost, nnode));
    }
  }
  return -1;
}

int main() {

  cin >> n >> m >> x;

  for (int i = 0; i < n; i++)
    scanf("%lld", &h[i]);

  ll a, b, c;
  for (int i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &a, &b, &c);
    a--, b--;
    if (h[a] >= c)
      G[a].push_back(P(b, c));
    if (h[b] >= c)
      G[b].push_back(P(a, c));
  }

  cout << dijkstra() << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define INF (1e17)
#define N 100005
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;

ll n, m, x, h[N];
vector<P> G[N];
priority_queue<P1, vector<P1>, greater<P1>> q;
P d[N];

ll dijkstra() {

  for (int i = 0; i < n; i++)
    d[i].first = INF;

  d[0] = P(0, x);
  q.push(P1(0, P(0, x)));

  while (!q.empty()) {
    P1 temp = q.top();
    q.pop();
    ll cost = temp.first;
    ll node = temp.second.first;
    ll a = temp.second.second;

    if (node == n - 1)
      return cost;
    if (d[node].first < cost)
      continue;

    for (int i = 0; i < G[node].size(); i++) {
      ll nnode = G[node][i].first;
      ll t = G[node][i].second;
      ll h1 = h[node], h2 = h[nnode];
      ll ncost, na;

      if (a - t < 0) {
        ncost = cost + (t + (t - a));
        if (t > h1)
          continue;
        else
          na = 0;
      } else if (a - t > h2) {
        ncost = cost + (t + (a - (h2 + t)));
        na = h2;
      } else {
        ncost = cost + t;
        na = a - t;
      }

      if (nnode == n - 1) {
        ncost += abs(h2 - na);
        na = h2;
      }

      if (d[nnode].first != INF) {

        ll S = abs(na - d[node].first);

        if (d[node].first + S > ncost)
          d[node] = P(ncost, na);
        else
          continue;

      } else
        d[nnode] = P(ncost, na);

      q.push(P1(ncost, P(nnode, na)));
    }
  }
  return INF;
}

int main() {

  cin >> n >> m >> x;

  for (int i = 0; i < n; i++)
    scanf("%lld", &h[i]);

  ll a, b, c;
  for (int i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &a, &b, &c);
    a--, b--;
    G[a].push_back(P(b, c));
    G[b].push_back(P(a, c));
  }

  ll ans = dijkstra();
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

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

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

using LL = long long;

struct Edge {
  LL from, to, t;
};
struct Tree {
  LL height;
  vector<Edge> edge;
};

int main() {
  int N, M;
  LL X;
  cin >> N >> M >> X;

  vector<Tree> tree(N);
  for (auto &i : tree)
    cin >> i.height;

  for (int i = 0; i < M; ++i) {
    int A, B, T;
    cin >> A >> B >> T;
    --A;
    --B;
    if (T <= tree[A].height)
      tree[A].edge.push_back({A, B, T});
    if (T <= tree[B].height)
      tree[B].edge.push_back({B, A, T});
  }

  vector<LL> d(N, -1);
  using Q = tuple<LL, LL, int>;
  priority_queue<Q, vector<Q>, greater<Q>> q;
  q.emplace(0, -X, 0);
  while (!q.empty()) {
    LL t, u;
    int v;
    tie(t, u, v) = q.top();
    q.pop();
    if (v == N - 1) {
      auto c = X + t + t + tree[v].height + u + u;
      if (d[v] == -1)
        d[v] = c;
      else
        d[v] = min(d[v], c);
      continue;
    }
    if (d[v] != -1 && d[v] <= t + u)
      continue;
    d[v] = t + u;
    for (auto &e : tree[v].edge)
      q.emplace(t + e.t, max({u, -X, -(t + e.t + tree[e.to].height)}), e.to);
  }

  cout << d[N - 1] << endl;
}

Error: MLE

Fixed submission:

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

using LL = long long;

struct Edge {
  LL from, to, t;
};
struct Tree {
  LL height;
  vector<Edge> edge;
};

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

  vector<Tree> tree(N);
  for (auto &i : tree)
    cin >> i.height;

  for (int i = 0; i < M; ++i) {
    int A, B, T;
    cin >> A >> B >> T;
    --A;
    --B;
    tree[A].edge.push_back({A, B, T});
    tree[B].edge.push_back({B, A, T});
  }

  // ??????h?????????e???????????¨?????????????????¨????????°????????????
  auto need = [&](LL h, Edge &e) {
    using T = tuple<LL, LL>;
    // ?????????????£??????§????±????????????´???
    if (tree[e.from].height < e.t)
      return T(-1, -1);
    // ?????£????£???¶
    if (h < e.t)
      return T(e.t + e.t - h, 0);
    // ??????????£???¶
    if (tree[e.to].height < h - e.t)
      return T(e.t + h - e.t - tree[e.to].height, tree[e.to].height);
    // ????????´???????£???¶
    return T(e.t, h - e.t);
  };

  using Memo = tuple<int, int>; // height, tree
  map<Memo, LL> memo;

  using Node = tuple<LL, int, int>; // time, height, tree
  priority_queue<Node, vector<Node>, greater<Node>> q;
  q.emplace(0, X, 0);
  while (!q.empty()) {
    LL t, h, v;
    tie(t, h, v) = q.top();
    q.pop();

    if (memo.count(Memo(h, v)))
      continue;
    memo[Memo(h, v)] = t;

    if (v == N - 1) {
      if (h == tree[v].height)
        break;
      if (!memo.count(Memo(tree[v].height, v)))
        q.emplace(t + tree[v].height - h, tree[v].height, v);
      continue;
    }

    for (auto &e : tree[v].edge) {
      LL d;
      int nh;
      tie(d, nh) = need(h, e);
      if (d == -1)
        continue;
      if (memo.count(Memo(nh, e.to)))
        continue;
      q.emplace(t + d, nh, e.to);
    }
  }

  if (memo.count(Memo(tree[N - 1].height, N - 1)))
    cout << memo[Memo(tree[N - 1].height, N - 1)] << endl;
  else
    cout << -1 << endl;
}
Problem p00524 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
static const int INF = 1LL << 61;
static const int MAX_N = 100005;

struct E {
  int to, cost;
  E() {}
  E(int p1, int p2) {
    to = p1;
    cost = p2;
  }
};

int n, m, x, H[MAX_N], d[MAX_N];
vector<E> G[MAX_N];
priority_queue<pii, vector<pii>, greater<pii>> q;

void dijkstra() {
  fill(d, d + MAX_N, INF);
  d[1] = 0;
  q.push(pii(0, 1));

  while (q.size()) {
    pii p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    int h = max(0LL, x - p.first);

    for (int i = 0; i < G[v].size(); ++i) {
      int to = G[v][i].to, cost = G[v][i].cost, ncost = cost + p.first;
      if (h < cost)
        ncost += cost - h;
      else if (H[to] < h - cost)
        ncost += h - H[to] - cost;

      if (d[to] <= ncost)
        continue;
      d[to] = ncost;
      q.push(pii(ncost, to));
    }
  }

  cout << ((d[n] == INF) ? -1 : d[n] + (H[n] - max(0LL, x - d[n]))) << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m >> x;
  for (int i = 1; i <= n; ++i) {
    cin >> H[i];
  }

  for (int i = 0; i < m; ++i) {
    int a, b, t;
    cin >> a >> b >> t;
    if (0 <= H[a] - t)
      G[a].PB(E(b, t));
    if (0 <= H[b] - t)
      G[b].PB(E(a, t));
  }

  dijkstra();

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
static const int INF = 1LL << 61;
static const int MAX_N = 100005;

struct E {
  int to, cost;
  E() {}
  E(int p1, int p2) {
    to = p1;
    cost = p2;
  }
};

int n, m, x, H[MAX_N], d[MAX_N];
vector<E> G[MAX_N];
priority_queue<pii, vector<pii>, greater<pii>> q;

void dijkstra() {
  fill(d, d + MAX_N, INF);
  d[1] = 0;
  q.push(pii(0, 1));

  while (q.size()) {
    pii p = q.top();
    q.top();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    int h = max(0LL, x - p.first);

    for (int i = 0; i < G[v].size(); ++i) {
      int to = G[v][i].to, cost = G[v][i].cost, ncost = cost;
      if (h <= cost)
        ncost += cost;
      else if (H[to] < h - cost)
        ncost += H[to] - cost;

      if (d[to] < ncost)
        continue;
      q.push(pii(ncost, to));
    }
  }

  cout << ((d[n] == INF) ? -1 : d[n]) << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m >> x;
  for (int i = 1; i <= n; ++i) {
    cin >> H[i];
  }

  for (int i = 0; i < m; ++i) {
    int a, b, t;
    cin >> a >> b >> t;
    if (0 <= H[a] - t)
      G[a].PB(E(b, t));
    if (0 <= H[b] - t)
      G[b].PB(E(a, t));
  }

  dijkstra();

  return 0;
}