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

// TLE

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const long N = 100000;

int main() {
  long n, w, h;
  long tate[N + 1], yoko[N + 1];

  memset(tate, 0, sizeof(tate));
  memset(yoko, 0, sizeof(yoko));

  scanf(" %ld %ld %ld", &n, &w, &h);

  for (long i = 0; i < n; ++i) {
    long x, y, z;
    scanf(" %ld %ld %ld", &x, &y, &z);

    // tate direct
    long ta = x - z + 1, tb = x + z;
    if (ta < 1)
      ta = 1;
    if (tb > w)
      tb = w;
    tate[ta]++;
    tate[tb + 1]--;

    // yoko direct
    long ya = y - z + 1, yb = y + z;
    if (ya < 1)
      ya = 1;
    if (yb > h)
      yb = h;
    yoko[ya]++;
    yoko[yb + 1]--;
  }

  for (long i = 0; i <= w; ++i)
    tate[i + 1] += tate[i];
  for (long i = 0; i <= h; ++i)
    yoko[i + 1] += yoko[i];

  /*
  for(long i=0; i<=w; ++i) printf("tate[%ld]=%d\\n", i, tate[i]);
  for(long i=0; i<=h; ++i) printf("yoko[%ld]=%d\\n", i, yoko[i]);
  */

  bool ans = true;
  for (long i = 1; i <= w; ++i) {
    if (tate[i] <= 0) {
      ans = false;
      break;
    }
  }

  if (!ans) {
    ans = true;
    for (long i = 1; i <= h; ++i) {
      if (yoko[i] <= 0) {
        ans = false;
        break;
      }
    }
  }
  if (ans)
    printf("Yes\\n");
  else
    printf("No\\n");
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const long N = 100000;

int main() {
  long n, w, h;
  int tate[N + 1], yoko[N + 1];

  memset(tate, 0, sizeof(tate));
  memset(yoko, 0, sizeof(yoko));

  scanf(" %ld %ld %ld", &n, &w, &h);
  for (long i = 0; i < n; ++i) {
    long x, y, z;
    scanf(" %ld %ld %ld", &x, &y, &z);

    // tate direct
    for (long j = x - z; j < x + z; ++j) {
      if (j < 0)
        continue;
      else if (j >= w)
        break;
      tate[j] = 1;
    }
    // yoko direct
    for (long j = y - z; j < y + z; ++j) {
      if (j < 0)
        continue;
      else if (j >= h)
        break;
      yoko[j] = 1;
    }
  }

  /*
  for(long i=0; i<w; ++i) printf("tate[%ld]=%d\\n", i, tate[i]);
  for(long i=0; i<h; ++i) printf("yoko[%ld]=%d\\n", i, yoko[i]);
  */

  bool ans = true;
  for (long i = 0; i < w; ++i) {
    if (tate[i] == 0) {
      ans = false;
      break;
    }
  }

  if (!ans) {
    ans = true;
    for (long i = 0; i < h; ++i) {
      if (yoko[i] == 0) {
        ans = false;
        break;
      }
    }
  }
  if (ans)
    printf("Yes\\n");
  else
    printf("No\\n");
}
Problem p01712 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, W, H;
  cin >> N >> W >> H;

  int vertical[W + 1];
  int horizon[H + 1];
  for (int i = 0; i < W + 1; i++) {
    vertical[i] = 0;
  }
  for (int i = 0; i < H + 1; i++) {
    horizon[i] = 0;
  }

  for (int times = 0; times < N; times++) {
    int x, y, w;
    cin >> x >> y >> w;
    int temp;
    temp = (x - w >= 0) ? x - w : 0;
    vertical[temp]++;
    temp = (x + w <= W) ? x + w : W;
    vertical[temp]--;

    temp = (y - w >= 0) ? y - w : 0;
    horizon[temp]++;
    temp = (y + w <= H) ? y + w : H;
    horizon[temp]--;
  }

  for (int i = 1; i < W + 1; i++) {
    vertical[i] += vertical[i - 1];
  }
  for (int i = 1; i < H + 1; i++) {
    horizon[i] += horizon[i - 1];
  }
  bool flagv = true;
  bool flagh = true;
  for (int i = 0; i < W; i++) {
    if (vertical[i] == 0) {
      flagv = false;
      break;
    }
  }
  for (int i = 0; i < H; i++) {
    if (horizon[i] == 0) {
      flagh = false;
      break;
    }
  }

  if (!flagv && !flagh)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N, W, H;
  cin >> N >> W >> H;

  int vertical[W + 1];
  int horizon[H + 1];
  for (int i = 0; i < W + 1; i++) {
    vertical[i] = 0;
  }
  for (int i = 0; i < H + 1; i++) {
    horizon[i] = 0;
  }

  for (int times = 0; times < N; times++) {
    int x, y, w;
    cin >> x >> y >> w;
    int temp;
    temp = (x - w >= 0) ? x - w : 0;
    vertical[temp]++;
    temp = (x + w <= W) ? x + w : W;
    vertical[temp]--;

    temp = (y - w >= 0) ? y - w : 0;
    horizon[temp]++;
    temp = (y + w <= H) ? y + w : H;
    vertical[temp]--;
  }

  for (int i = 1; i < W + 1; i++) {
    vertical[i] += vertical[i - 1];
  }
  for (int i = 1; i < H + 1; i++) {
    horizon[i] += horizon[i - 1];
  }
  bool flagv = true;
  bool flagh = true;
  for (int i = 0; i < W; i++) {
    if (vertical[i] == 0) {
      flagv = false;
      break;
    }
  }
  for (int i = 0; i < H; i++) {
    if (horizon[i] == 0) {
      flagh = false;
      break;
    }
  }

  if (!flagv && !flagh)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;

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

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

int N, W, H;
int sumx[100010];
int sumy[100010];

int main() {

  cin >> N >> W >> H;
  rep(i, N) {
    int x, y, w;
    cin >> x >> y >> w;
    sumx[max(0, x - w)]++, sumx[min(W + 1, x + w)]--;
    sumy[max(0, y - w)]++, sumy[min(H + 1, y + w)]--;
  }
  REP(i, 1, 100010) {
    sumx[i] += sumx[i - 1];
    sumy[i] += sumy[i - 1];
  }

  bool ok = true;
  rep(i, W) {
    if (sumx[i] == 0)
      ok = false;
  }
  if (!ok) {
    ok = true;
    rep(i, H) {
      if (sumy[i] == 0)
        ok = false;
    }
  }

  cout << (ok ? "Yes" : "No") << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

int N, W, H;
int sumx[10010];
int sumy[10010];

int main() {

  cin >> N >> W >> H;
  rep(i, N) {
    int x, y, w;
    cin >> x >> y >> w;
    sumx[max(0, x - w)]++, sumx[min(W + 1, x + w + 1)]--;
    sumy[max(0, y - w)]++, sumy[min(H + 1, y + w + 1)]--;
  }
  REP(i, 1, 10010) {
    sumx[i] += sumx[i - 1];
    sumy[i] += sumy[i - 1];
  }

  bool ok = true;
  rep(i, W + 1) {
    if (sumx[i] == 0)
      ok = false;
  }
  if (!ok) {
    ok = true;
    rep(i, H + 1) {
      if (sumy[i] == 0)
        ok = false;
    }
  }

  cout << (ok ? "Yes" : "No") << endl;

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

#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> P;
typedef long long ll;

// ??§????????°INF
const int INF = (1 << 30);
// ?¨±?????????????????
const double EPS = (1e-10);
// 2??????????????????????????????????????????
#define EQ(a, b) (abs((a) - (b)) < EPS)

int main() {
  cin.tie(0);
  ios::ios_base::sync_with_stdio(false);
  int N, W, H;
  cin >> N >> W >> H;
  vector<bool> w((W << 1) + 1, false), h((H << 1) + 1, false);
  int x, y, r;
  for (int i = 0; i < N; ++i) {
    cin >> x >> y >> r;
    fill(w.begin() + max(0, (x - r) << 1),
         w.begin() + min(W << 1, (x + r) << 1) + 1, true);
    fill(h.begin() + max(0, (y - r) << 1),
         h.begin() + min(H << 1, (y + r) << 1) + 1, true);
  }
  for (int j = 1; j <= (W << 1) - 1; j += 2) {
    if (w[j])
      continue;
    for (int k = 1; k <= (H << 1) - 1; k += 2) {
      if (!h[k]) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> P;
typedef long long ll;

// ??§????????°INF
const int INF = (1 << 30);
// ?¨±?????????????????
const double EPS = (1e-10);
// 2??????????????????????????????????????????
#define EQ(a, b) (abs((a) - (b)) < EPS)

int main() {
  cin.tie(0);
  ios::ios_base::sync_with_stdio(false);
  int N, W, H;
  cin >> N >> W >> H;
  vector<bool> w((W << 1) + 1, false), h((H << 1) + 1, false);
  int x, y, r;
  for (int i = 0; i < N; ++i) {
    cin >> x >> y >> r;
    for (int tx = max(0, (x - r) << 1); tx <= min(W << 1, (x + r) << 1); ++tx)
      w[tx] = true;
    for (int ty = max(0, (y - r) << 1); ty <= min(H << 1, (y + r) << 1); ++ty)
      h[ty] = true;
  }
  for (int j = 0; j <= (W << 1) - 1; ++j) {
    if (w[j])
      continue;
    for (int k = 0; k <= (H << 1) - 1; ++k) {
      if (!h[k]) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p01712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> P;
typedef long long ll;

// ????§??????????°INF
const int INF = (1 << 30);
// ???¨??±?????????????????
const double EPS = (1e-10);
// 2??????????????????????????????????????????
#define EQ(a, b) (abs((a) - (b)) < EPS)

int main() {
  cin.tie(0);
  ios::ios_base::sync_with_stdio(false);
  int N, W, H;
  cin >> N >> W >> H;
  vector<bool> w((W << 1) + 1, false), h((H << 1) + 1, false);
  int x, y, r;
  for (int i = 0; i < N; ++i) {
    cin >> x >> y >> r;
    fill(w.begin() + max(0, (x - r) << 1),
         w.begin() + min(W << 1, (x + r) << 1) + 1, true);
    fill(h.begin() + max(0, (y - r) << 1),
         h.begin() + min(H << 1, (y + r) << 1) + 1, true);
  }
  for (int j = 1; j <= (W << 1) - 1; j += 2) {
    if (w[j])
      continue;
    for (int k = 1; k <= (H << 1) - 1; k += 2) {
      if (!h[k]) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr);
  ios::ios_base::sync_with_stdio(false);
  int N, W, H;
  cin >> N >> W >> H;
  vector<bool> w((W << 1) + 1, false), h((H << 1) + 1, false);
  int x, y, r;
  for (int i = 0; i < N; ++i) {
    cin >> x >> y >> r;
    fill(w.begin() + max(0, (x - r) << 1),
         w.begin() + min(W << 1, (x + r) << 1) + 1, true);
    fill(h.begin() + max(0, (y - r) << 1),
         h.begin() + min(H << 1, (y + r) << 1) + 1, true);
  }
  if (W < H) {
    for (int j = 1; j <= (W << 1) - 1; j += 2) {
      if (w[j])
        continue;
      for (int k = 1; k <= (H << 1) - 1; k += 2) {
        if (!h[k]) {
          cout << "No" << endl;
          return EXIT_SUCCESS;
        }
      }
    }
  } else {
    for (int j = 1; j <= (H << 1) - 1; j += 2) {
      if (h[j])
        continue;
      for (int k = 1; k <= (W << 1) - 1; k += 2) {
        if (!w[k]) {
          cout << "No" << endl;
          return EXIT_SUCCESS;
        }
      }
    }
  }
  cout << "Yes" << endl;
  return EXIT_SUCCESS;
}
Problem p01712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(0);
  ios::ios_base::sync_with_stdio(false);
  int N, W, H;
  cin >> N >> W >> H;
  vector<bool> w((W << 1) + 1, false), h((H << 1) + 1, false);
  int x, y, r;
  for (int i = 0; i < N; ++i) {
    cin >> x >> y >> r;
    fill(w.begin() + max(0, (x - r) << 1),
         w.begin() + min(W << 1, (x + r) << 1) + 1, true);
    fill(h.begin() + max(0, (y - r) << 1),
         h.begin() + min(H << 1, (y + r) << 1) + 1, true);
  }
  for (int j = 1; j <= (W << 1) - 1; j += 2) {
    if (w[j])
      continue;
    for (int k = 1; k <= (H << 1) - 1; k += 2) {
      if (!h[k]) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> P;
typedef long long ll;

constexpr int INF = (1 << 30);
constexpr double EPS = (1e-6);
//????´???°??????????????§?¨???¨????????¨????????¨??????a-b???????????¢
#define EQ(a, b) (abs((a) - (b)) < EPS)

int main() {
  cin.tie(nullptr);
  ios::ios_base::sync_with_stdio(false);
  int N, W, H;
  cin >> N >> W >> H;
  vector<bool> w((W << 1) + 1, false), h((H << 1) + 1, false);
  int x, y, r;
  for (int i = 0; i < N; ++i) {
    cin >> x >> y >> r;
    fill(w.begin() + max(0, (x - r) << 1),
         w.begin() + min(W << 1, (x + r) << 1) + 1, true);
    fill(h.begin() + max(0, (y - r) << 1),
         h.begin() + min(H << 1, (y + r) << 1) + 1, true);
  }
  if (W > H) {
    for (int j = 1; j <= (W << 1) - 1; j += 2) {
      if (w[j])
        continue;
      for (int k = 1; k <= (H << 1) - 1; k += 2) {
        if (!h[k]) {
          cout << "No" << endl;
          return EXIT_SUCCESS;
        }
      }
    }
  } else {
    for (int j = 1; j <= (H << 1) - 1; j += 2) {
      if (h[j])
        continue;
      for (int k = 1; k <= (W << 1) - 1; k += 2) {
        if (!w[k]) {
          cout << "No" << endl;
          return EXIT_SUCCESS;
        }
      }
    }
  }
  cout << "Yes" << endl;
  return EXIT_SUCCESS;
}
Problem p01713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;
int W;
vector<int> a(W);

void exit_right(int s, int t, vector<int> &r) {
  for (int i = s; i < t; ++i) {
    if (a[i] > 0) {
      r[i] = a[i];
    } else if (a[i] < 0) {
      for (int j = s; j < i; ++j) {
        if (r[j] > 0) {
          int temp = (i - j) + a[i];
          r[j] = min(r[j], -temp);
        }
      }
    }
  }
}

void exit_left(int s, int t, vector<int> &l) {
  for (int i = t; i > s; --i) {
    if (a[i] > 0) {
      l[i] = a[i];
    } else if (a[i] < 0) {
      for (int j = i + 1; j < t; ++j) {
        if (l[j] > 0) {
          int temp = (j - i) + a[i];
          l[j] = min(l[j], -temp);
        }
      }
    }
  }
}

int main(void) {
  cin >> W;
  vector<int> exit;
  vector<bool> wall;
  int temp;
  bool find = false;
  for (int i = 0; i < W; ++i) {
    cin >> temp;
    a.push_back(temp);
    if (!find && a[i] < 0) {
      find = true;
    }
    if (a[i] == 0) {
      exit.push_back(i);
      wall.push_back(find);
      find = false;
    }
  }

  int res = 0;
  if (exit.size() > 0) {
    vector<int> r(W, 0), l(W, 0);
    int size = exit.size();
    for (int i = 0; i < size + 1; ++i) {
      if (i == 0 || i == size) {
        if (i == 0) {
          exit_right(i, exit[i], r);
        }
        if (i == size) {
          exit_left(exit[i - 1], W, l);
        }
      } else {
        exit_right(exit[i - 1], exit[i], r);
        if (wall[i]) {
          exit_left(exit[i - 1], exit[i], l);
        }
      }
    }

    for (int i = 0; i < W; ++i) {
      if (r[i] > 0 || l[i] > 0) {
        res += max(r[i], l[i]);
      }
    }
  }

  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int W;
  cin >> W;
  vector<int> a(W), s;
  int exit = false;
  for (int i = 0; i < W; ++i) {
    cin >> a[i];
    if (a[i] == 0) {
      ++exit;
    }
  }

  int res = 0;
  if (exit > 0) {
    vector<int> r(W, 0), l(W, 0);
    int ce, ind;
    ce = ind = 0;
    for (int i = 0; ce < exit && i < W; ++i) {
      r[i] = a[i];
      if (a[i] < 0) {
        for (int j = ind; j < i; ++j) {
          if (a[j] > 0) {
            if (r[j] > 0) {
              int temp = (i - j) + a[i];
              r[j] = min(r[j], -temp);
            }
          }
        }
      }
      if (a[i] == 0) {
        ind = i + 1;
        ++ce;
      }
    }
    ce = 0;
    ind = W - 1;
    for (int i = W - 1; ce < exit && i >= 0; --i) {
      l[i] = a[i];
      if (a[i] < 0) {
        for (int j = ind; j > i; --j) {
          if (a[j] > 0) {
            if (l[j] > 0 && l[j] > r[j]) {
              int temp = (j - i) + a[i];
              l[j] = min(l[j], -temp);
            }
          }
        }
      }
      if (a[i] == 0) {
        ind = i - 1;
        ++ce;
      }
    }

    for (int i = 0; i < W; ++i) {
      if (r[i] > 0 || l[i] > 0) {
        res += max(r[i], l[i]);
      }
    }
  }

  cout << res << endl;

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

#include <iostream>
using namespace std;

int W;
int A[100010];
int L[100010] = {}, R[100010] = {};
int inf = 2e9;
int main() {
  cin >> W;
  for (int i = 1; i <= W; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= W; i++) {
    if (A[i] == 0)
      L[i] = inf;
    else if (A[i] > 0)
      L[i] = max(L[i - 1] - 1, 0);
    else
      L[i] = min(max(L[i - 1] - 1, 0), -A[i]);
  }
  for (int i = W; i >= 1; i--) {
    if (A[i] == 0)
      R[i] = inf;
    else if (A[i] > 0)
      R[i] = max(R[i + 1] - 1, 0);
    else
      R[i] = min(max(R[i + 1] - 1, 0), -A[i]);
  }
  int ans = 0;
  /*    for(int i=1;i<=W;i++) cerr << L[i] << " ";
      cerr << endl;
      for(int i=1;i<=W;i++) cerr << R[i] << " ";
      cerr << endl;
  */
  for (int i = 1; i <= W; i++) {
    if (A[i] <= 0)
      continue;
    int l = 0, r = 0;
    ans += min(A[i], max(L[i], R[i]));
  }
  cout << ans << endl;
}


Error: 0
0 2000000000 2 1 0 0 2000000000 
1999999999 2000000000 0 0 1 2 2000000000 


Fixed submission:

#include <iostream>
using namespace std;

int W;
int A[100010];
int L[100010], R[100010];
int inf = 2e9;
int main() {
  cin >> W;
  for (int i = 1; i <= W; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= W; i++) {
    if (L[i - 1] == 0 && A[i] == 0)
      L[i] = inf;
    else {
      if (A[i] == 0)
        L[i] = max(L[i - 1] - 1, 0);
      else if (A[i] > 0)
        L[i] = max(L[i - 1] - 1, 0);
      else
        L[i] = min(max(L[i - 1] - 1, 0), -A[i]);
    }
  }
  for (int i = W; i >= 1; i--) {
    if (R[i + 1] == 0 && A[i] == 0)
      R[i] = inf;
    else {
      if (A[i] == 0)
        R[i] = max(R[i + 1] - 1, 0);
      else if (A[i] > 0)
        R[i] = max(R[i + 1] - 1, 0);
      else
        R[i] = min(max(R[i + 1] - 1, 0), -A[i]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= W; i++)
    cerr << L[i] << " ";
  cerr << endl;
  for (int i = 1; i <= W; i++)
    cerr << R[i] << " ";
  cerr << endl;
  for (int i = 1; i <= W; i++) {
    if (A[i] <= 0)
      continue;
    int l = 0, r = 0;
    ans += min(A[i], max(L[i], R[i]));
  }
  cout << ans << endl;
}
Problem p01713 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int W;
  cin >> W;
  vector<int> a(W + 2), dp1(W + 2, -1e9), dp2(W + 2, -1e9);
  for (int i = 1; i <= W; i++)
    cin >> a[i];

  for (int i = 1; i <= W; i++) {
    if (a[i] == 0)
      dp1[i] = 1e9;
    else if (a[i] < 0)
      dp1[i] = min(dp1[i - 1] - 1, -a[i]);
    else
      dp1[i] = dp1[i - 1] - 1;
  }
  for (int i = W; i >= 1; i--) {
    if (a[i] == 0)
      dp2[i] = 1e9;
    else if (a[i] < 0)
      dp2[i] = min(dp2[i + 1] - 1, -a[i]);
    else
      dp2[i] = dp2[i + 1] - 1;
  }

  int ans = 0;
  for (int i = 1; i <= W; i++)
    ans += max(0, min(max(dp1[i], dp2[i]), a[i]));
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int W;
  cin >> W;
  vector<int> a(W), dp1(W + 2, -1e9), dp2(W + 2, -1e9);
  for (int i = 1; i <= W; i++)
    cin >> a[i];

  for (int i = 1; i <= W; i++) {
    if (a[i] == 0)
      dp1[i] = 1e9;
    else if (a[i] < 0)
      dp1[i] = min(dp1[i - 1] - 1, -a[i]);
    else
      dp1[i] = dp1[i - 1] - 1;
  }
  for (int i = W; i >= 1; i--) {
    if (a[i] == 0)
      dp2[i] = 1e9;
    else if (a[i] < 0)
      dp2[i] = min(dp2[i + 1] - 1, -a[i]);
    else
      dp2[i] = dp2[i + 1] - 1;
  }

  int ans = 0;
  for (int i = 1; i <= W; i++)
    ans += max(0, min(max(dp1[i], dp2[i]), a[i]));
  cout << ans << endl;
}
Problem p01713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define WMAX 100000
#define ESC_UNLIMITED 10001
using namespace std;

int main() {
  short a_list[WMAX], leftesc[WMAX] = {};
  int w;
  cin >> w;

  short esc_limit = 0;
  for (int i = 0; i < w; i++) {
    cin >> a_list[i];
    auto &a = a_list[i];
    if (a == 0) {
      esc_limit = ESC_UNLIMITED;
    } else if (esc_limit > 0) {
      if (a > 0) {
        leftesc[i] = min(a, esc_limit);
      } else {
        esc_limit = min(esc_limit, (short)-a);
      }
      if (esc_limit != ESC_UNLIMITED)
        esc_limit--;
    }
  }

  unsigned long r = 0;
  esc_limit = 0;
  for (int i = w - 1; i >= 0; i--) {
    auto &a = a_list[i];
    if (a == 0) {
      esc_limit = ESC_UNLIMITED;
    } else if (esc_limit > 0) {
      if (a > 0) {
        r += max(leftesc[i], min(a, esc_limit));
      } else {
        esc_limit = min(esc_limit, (short)-a);
      }
      if (esc_limit != ESC_UNLIMITED)
        esc_limit--;
    } else if (a > 0) {
      r += leftesc[i];
    }
  }

  cout << r << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#define NOTHING 2 << 31
using namespace std;
typedef map<int, int> M;
typedef set<int> S;

inline int zero_if_nothing(int num) { return num == NOTHING ? 0 : num; }

int main() {
  int w, a;
  cin >> w;
  M ents, shuts;
  S escs;
  for (int i = 0; i < w; i++) {
    cin >> a;
    if (a > 0) {
      ents[i] = a;
    } else if (a < 0) {
      shuts[i] = -a;
    } else {
      escs.insert(i);
    }
  }

  int r = 0;
  for (auto &&ent : ents) {
    auto right_escit = escs.upper_bound(ent.first);
    auto left_escit = right_escit;
    if (right_escit != escs.begin()) {
      left_escit--;
    } else {
      left_escit = escs.end();
    }
    auto right_esc = right_escit != escs.end() ? *right_escit : ent.first;
    auto left_esc = left_escit != escs.end() ? *left_escit : ent.first;
    int r_by_side[2] = {left_esc == ent.first ? NOTHING : ent.second,
                        right_esc == ent.first ? NOTHING : ent.second};
    for (auto shutit = shuts.upper_bound(left_escit != escs.end() ? *left_escit
                                                                  : ent.first);
         shutit != shuts.end() && shutit->first < right_esc; shutit++) {
      ent.first;
      bool side = ent.first < shutit->first;
      r_by_side[side] =
          min(r_by_side[side],
              min(max(shutit->second - abs(ent.first - shutit->first), 0),
                  ent.second));
    }
    r += max(zero_if_nothing(r_by_side[0]), zero_if_nothing(r_by_side[1]));
  }

  cout << r << endl;
  return 0;
}