prithivMLmods/Coder-Stat huggingface dataset:
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<long long int> D(N);

  vector<long long int> Juwel(N);
  for (int i = 0; i < N; i++) {
    cin >> D.at(i);
  }
  int answer = 0, sum = 0;
  for (int action = 0; action <= K; action++) {
    for (int back = 0; back <= action; back++) {
      for (int left = 0; left <= action - back; left++) {
        Juwel.clear();
        sum = 0;
        if (left < (int)D.size()) {
          for (int i = 0; i < left; i++)
            Juwel.push_back(D.at(i));

          for (int i = 0; i < action - back - left; i++)
            Juwel.push_back(D.at(N - i - 1));
        } else {
          for (int i = 0; i < (int)D.size(); i++)
            Juwel.push_back(D.at(i));
        }

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

        for (int i = 0; i < min(back, (int)Juwel.size()); i++) {
          if (Juwel.at(i) < 0)
            Juwel.at(i) = 0;
        }
        for (int i = 0; i < (int)Juwel.size(); i++)
          sum += Juwel.at(i);

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

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<long long int> D(N);

  vector<long long int> Juwel(N);
  for (int i = 0; i < N; i++) {
    cin >> D.at(i);
  }
  int answer = 0, sum = 0;
  for (int action = 0; action <= K; action++) {
    for (int back = 0; back <= action; back++) {
      for (int left = 0; left <= action - back; left++) {
        Juwel.clear();
        sum = 0;
        if (left < (int)D.size()) {
          for (int i = 0; i < left; i++)
            Juwel.push_back(D.at(i));

          for (int i = 0; i < min(action - back - left, N - back - left); i++)
            Juwel.push_back(D.at(N - i - 1));
        } else {
          for (int i = 0; i < (int)D.size(); i++)
            Juwel.push_back(D.at(i));
        }

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

        for (int i = 0; i < min(back, (int)Juwel.size()); i++) {
          if (Juwel.at(i) < 0)
            Juwel.at(i) = 0;
        }
        for (int i = 0; i < (int)Juwel.size(); i++)
          sum += Juwel.at(i);

        if (sum >= answer)
          answer = sum;
      }
    }
  }
  cout << answer << endl;
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
// 2020-07-15 23:13:36
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

vector<int> vs;
int dp[55][55][105];
int rec(int l, int r, int k) {
  if (dp[l][r][k] >= 0)
    return dp[l][r][k];
  chmax(dp[l][r][k], 0);
  if (k >= 1) {
    chmax(dp[l][r][k], rec(l + 1, r, k - 1) + vs[l]);
    chmax(dp[l][r][k], rec(l, r - 1, k - 1) + vs[r - 1]);
  }
  if (k >= 2) {
    chmax(dp[l][r][k], rec(l + 1, r, k - 2));
    chmax(dp[l][r][k], rec(l, r - 1, k - 2));
  }
  return dp[l][r][k];
}
void answer() {
  int n, k;
  cin >> n >> k;
  vs.resize(n);
  rep(i, n) cin >> vs[i];
  repc(i, n) repc(j, n) repc(ki, k) dp[i][j][ki] = -1;
  cout << rec(0, n, k) << '\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

Fixed submission:
// 2020-07-15 23:13:36
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

vector<int> vs;
int dp[55][55][105];
int rec(int l, int r, int k) {
  if (dp[l][r][k] >= 0)
    return dp[l][r][k];
  chmax(dp[l][r][k], 0);
  if (l == r)
    return dp[l][r][k];
  if (k >= 1) {
    chmax(dp[l][r][k], rec(l + 1, r, k - 1) + vs[l]);
    chmax(dp[l][r][k], rec(l, r - 1, k - 1) + vs[r - 1]);
  }
  if (k >= 2) {
    chmax(dp[l][r][k], rec(l + 1, r, k - 2));
    chmax(dp[l][r][k], rec(l, r - 1, k - 2));
  }
  return dp[l][r][k];
}
void answer() {
  int n, k;
  cin >> n >> k;
  vs.resize(n);
  rep(i, n) cin >> vs[i];
  repc(i, n) repc(j, n) repc(ki, k) dp[i][j][ki] = -1;
  cout << rec(0, n, k) << '\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  int maxvalue = 0;
  for (int take = k / 2; take <= k; take++) {
    for (int left = 0; left <= take; left++) {
      vector<int> jewels(take);
      for (int i = 0; i < left; i++) {
        jewels.at(i) = v.at(i);
      }
      for (int i = left; i < take; i++) {
        jewels.at(i) = v.at(i + n - take);
      }
      sort(jewels.begin(), jewels.end());
      for (int i = 0; i < min(take, k - take); i++) {
        jewels.at(i) = max(0, jewels.at(i));
      }
      int value = 0;
      for (int i = 0; i < take; i++) {
        value += jewels.at(i);
      }
      maxvalue = max(value, maxvalue);
    }
  }
  cout << maxvalue << endl;
}


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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  int maxvalue = 0;
  for (int take = min(n, k / 2); take <= min(n, k); take++) {
    for (int left = 0; left <= take; left++) {
      vector<int> jewels(take);
      for (int i = 0; i < left; i++) {
        jewels.at(i) = v.at(i);
      }
      for (int i = left; i < take; i++) {
        jewels.at(i) = v.at(i + n - take);
      }
      sort(jewels.begin(), jewels.end());
      for (int i = 0; i < min(take, k - take); i++) {
        jewels.at(i) = max(0, jewels.at(i));
      }
      int value = 0;
      for (int i = 0; i < take; i++) {
        value += jewels.at(i);
      }
      maxvalue = max(value, maxvalue);
    }
  }
  cout << maxvalue << endl;
}
Problem p03032 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define sorted(vector) sort(vector.begin(), vector.end())
#define vmax(vector) *max_element(vector.begin(), vector.end())
#define vsum(vector) accumulate(vector.begin(), vector.end(), 0)
#define append(a) push_back(a)
#define int(a) stoi(a)
#define str(a) to_string(a)

int main(void) {
  int n, k, tmp;
  vector<int> v, motimono, ans;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    v.push_back(tmp);
  }

  if (k >= n) {
    k -= n;
    ans.append(vsum(v));
    sorted(v);
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < min<unsigned long>(k, v.size()); i++) {
        v[j] = 0;
        ans.append(vsum(v));
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      v.append(v[i]);
    }
    for (int j = 1; j < k + 1; j++) {       // 長さ
      for (int i = n - j; i < n + 1; i++) { // 開始index
        vector<int> motimono;
        for (int r = i; r < i + j; r++) {
          motimono.append(v[r]);
        }
        ans.append(vsum(motimono));
        sorted(motimono);
        for (int i = 0; i < min<unsigned long>(k - j, motimono.size()); i++) {
          motimono[i] = 0;
          ans.append(vsum(motimono));
        }
      }
    }
  }
  cout << vmax(ans) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define sorted(vector) sort(vector.begin(), vector.end())
#define vmax(vector) *max_element(vector.begin(), vector.end())
#define vsum(vector) accumulate(vector.begin(), vector.end(), 0)
#define append(a) push_back(a)
#define int(a) stoi(a)
#define str(a) to_string(a)

int main(void) {
  int n, k, tmp;
  vector<int> v, motimono, ans;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    v.push_back(tmp);
  }

  if (k >= n) {
    k -= n;
    ans.append(vsum(v));
    sorted(v);
    for (int i = 0; i < min<unsigned long>(k, n); i++) {
      v[i] = 0;
      ans.append(vsum(v));
    }
  } else {
    for (int i = 0; i < n; i++) {
      v.append(v[i]);
    }
    for (int j = 1; j < k + 1; j++) {       // 長さ
      for (int i = n - j; i < n + 1; i++) { // 開始index
        vector<int> motimono;
        for (int r = i; r < i + j; r++) {
          motimono.append(v[r]);
        }
        ans.append(vsum(motimono));
        sorted(motimono);
        for (int i = 0; i < min<unsigned long>(k - j, motimono.size()); i++) {
          motimono[i] = 0;
          ans.append(vsum(motimono));
        }
      }
    }
  }
  cout << vmax(ans) << endl;
}
Problem p03032 (Python)

Buggy submission (status: Time Limit Exceeded):
import heapq

N, K = [int(_) for _ in input().split()]
V = [int(_) for _ in input().split()]
ans = 0
for il in range(min([N + 1, K + 1])):
    for ir in range(min([N + 1 - il, K + 1 - il])):
        for dl in range(il + 1):
            for dr in range(ir + 1):
                if il + ir + dl + dr > K:
                    continue
                HL = V[:il]
                HR = V[N - ir : N]
                heapq.heapify(HL)
                heapq.heapify(HR)
                value = 0
                for _ in range(dl):
                    v = heapq.heappop(HL)
                    if v >= 0:
                        value += v
                        break
                value += sum(HL)
                for _ in range(dr):
                    v = heapq.heappop(HR)
                    if v >= 0:
                        value += v
                        break
                value += sum(HR)
                ans = max([ans, value])
print(ans)


Fixed submission:
import heapq

N, K = [int(_) for _ in input().split()]
V = [int(_) for _ in input().split()]
ans = 0
for il in range(K + 1):
    for ir in range(K + 1 - il):
        if il + ir > min([N, K]):
            continue
        H = V[:il] + V[N - ir : N]
        heapq.heapify(H)
        now = 0
        for d in range(1, il + ir):
            if il + ir + d > K:
                break
            v = heapq.heappop(H)
            if v >= 0:
                now += v
                break
        now += sum(H)
        ans = max([ans, now])
print(ans)
Problem p03032 (Python)

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())
d = list(map(int, input().split()))
left = [0]
left_m = []
right = [0]
right_m = []
for i, l, r in zip(range(K), d, d[::-1]):
    left.append(left[i] + l)
    left_m.append(l if l < 0 else 0)
    right.append(right[i] + r)
    right_m.append(r if r < 0 else 0)
point = 0
for i in range(K):
    for j in range(K - i):
        p = K - i - j
        minus = list(sorted(left_m[:i] + right_m[:j]))
        point = max(point, left[i] + right[j] - sum(minus[:p]))
print(point)


Fixed submission:
N, K = map(int, input().split())
d = list(map(int, input().split()))
left = [0]
left_m = []
right = [0]
right_m = []
for i, l, r in zip(range(K), d, d[::-1]):
    left.append(left[i] + l)
    left_m.append(l if l < 0 else 0)
    right.append(right[i] + r)
    right_m.append(r if r < 0 else 0)
point = 0
m = min(N, K)
for i in range(m + 1):
    for j in range(m - i + 1):
        p = K - i - j
        minus = list(sorted(left_m[:i] + right_m[:j]))
        point = max(point, left[i] + right[j] - sum(minus[:p]))
print(point)
Problem p03032 (Python)

Buggy submission (status: Runtime Error):
def solve(string):
    n, k, *v = map(int, string.split())

    def dfs(deq, get, count, minus):
        if count == k:
            return sum(get)
        op1 = dfs(deq[1:], get + [deq[0]], count + 1, minus + (deq[0] < 0))
        op2 = dfs(deq[:-1], get + [deq[-1]], count + 1, minus + (deq[-1] < 0))
        m = min(get) if len(get) > 0 else 0
        op3 = 0 if m >= 0 else dfs(deq[:], sorted(get)[1:], count + 1, minus - 1)
        return max(op1, op2, op3)

    return str(dfs(v[:], [], 0, 0))


if __name__ == "__main__":
    print(solve("\n".join([input(), input()])))


Fixed submission:
def solve(string):
    n, k, *v = map(int, string.split())
    ans = 0
    for i in range(k + 1):
        for j in range(k - i + 1):
            base = sorted(v[:i] + v[max(n - k + i + j, i) :])
            ans = max(ans, sum([b for l, b in enumerate(base) if b >= 0 or j <= l]))
    return str(ans)


if __name__ == "__main__":
    print(solve("\n".join([input(), input()])))
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// 関数

// main関数
int main() {
  // 入力
  long N, K;
  cin >> N >> K;
  vector<long> V(N);
  for (int i = 0; i < N; i++) {
    cin >> V.at(i);
  }

  // 計算
  long ans = -1000000000000, Ls = 0;
  multiset<long> Lm;
  for (int L = 0; L <= N; L++) {
    if (L > 0) {
      Ls += V.at(L - 1);
      if (V.at(L - 1) < 0)
        Lm.insert(V.at(L - 1));
    }

    long Rs = 0;
    multiset<long> M = Lm;
    for (int R = 0; L + R <= K; R++) {
      if (R > 0) {
        Rs += V.at(N - R);
        if (V.at(N - R) < 0)
          M.insert(V.at(N - R));
      }

      long Pans = Ls + Rs;
      long count = 0;
      for (auto ite = M.begin(); (ite != M.end() && count < K - L - R); ite++) {
        Pans -= *ite;
        count++;
      }
      // cout<<"L"<<L<<"R"<<R<<"Pans"<<Pans<<endl;
      ans = max(Pans, ans);
    }
  }

  // 出力
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// 関数

// main関数
int main() {
  // 入力
  long N, K;
  cin >> N >> K;
  vector<long> V(N);
  for (int i = 0; i < N; i++) {
    cin >> V.at(i);
  }

  // 計算
  long ans = -1000000000000, Ls = 0;
  multiset<long> Lm;
  for (int L = 0; L <= N; L++) {
    if (L > 0) {
      Ls += V.at(L - 1);
      if (V.at(L - 1) < 0)
        Lm.insert(V.at(L - 1));
    }

    long Rs = 0;
    multiset<long> M = Lm;
    for (int R = 0; L + R <= min(K, N); R++) {
      if (R > 0) {
        Rs += V.at(N - R);
        if (V.at(N - R) < 0)
          M.insert(V.at(N - R));
      }

      long Pans = Ls + Rs;
      long count = 0;
      for (auto ite = M.begin(); (ite != M.end() && count < K - L - R); ite++) {
        Pans -= *ite;
        count++;
      }
      // cout<<"L"<<L<<"R"<<R<<"Pans"<<Pans<<endl;
      ans = max(Pans, ans);
    }
  }

  // 出力
  cout << ans << endl;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
template <typename X, typename Y>
istream &operator>>(istream &is, pair<X, Y> &p) {
  return is >> p.first >> p.second;
}
const int N = 50, K = 100;
ll mem[N + 1][N + 1][K + 1];
ll opt(int i, int j, int k, vector<int> &v) {
  ll &ans = mem[i + 1][j + 1][k];
  if (ans == -1) {
    if (i <= j) {
      ans = max({k >= 1 ? v[i] + opt(i + 1, j, k - 1, v) : 0,
                 k >= 1 ? v[j] + opt(i, j - 1, k - 1, v) : 0,
                 k >= 2 ? opt(i + 1, j, k - 2, v) : 0,
                 k >= 2 ? opt(i, j - 1, k - 2, v) : 0});
    } else {
      ans = 0;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  cin >> v;
  memset(mem, -1, sizeof mem);
  ll ans = 0;
  for (int kk = 0; kk <= k; kk++) {
    ans = max(ans, opt(0, n - 1, kk, v));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
template <typename X, typename Y>
istream &operator>>(istream &is, pair<X, Y> &p) {
  return is >> p.first >> p.second;
}
const int N = 50, K = 100;
ll mem[N + 2][N + 1][K + 1];
ll opt(int i, int j, int k, vector<int> &v) {
  ll &ans = mem[i + 1][j + 1][k];
  if (ans == -1) {
    if (i <= j) {
      ans = max({k >= 1 ? v[i] + opt(i + 1, j, k - 1, v) : 0,
                 k >= 1 ? v[j] + opt(i, j - 1, k - 1, v) : 0,
                 k >= 2 ? opt(i + 1, j, k - 2, v) : 0,
                 k >= 2 ? opt(i, j - 1, k - 2, v) : 0});
    } else {
      ans = 0;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  cin >> v;
  memset(mem, -1, sizeof mem);
  ll ans = 0;
  for (int kk = 0; kk <= k; kk++) {
    ans = max(ans, opt(0, n - 1, kk, v));
  }
  cout << ans << endl;
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define co(x) cout << x << '\n'
#define sz(x) (int)x.size()
#define all(a) a.begin(), a.end()
using namespace std;

bool cmp(int a, int b) { return a >= b; }

inline void solve() {
  int n, k, ans = 0;
  cin >> n >> k; // max(n,k) <= 100
  vector<int> a(n + 5);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i <= min(n, k); i++) {
    for (int j = 0; j <= i; j++) {
      int l = j, r = i - j, cur = 0;
      vector<int> hand;
      for (int t = 0; t < l; t++) {
        hand.pb(a[t]);
      }
      for (int t = 0; t < r; t++) {
        hand.pb(a[n - t - 1]);
      }
      sort(all(hand), cmp);
      for (int t = 1; t <= k - i and sz(hand) > 0; t++) {
        if (hand.back() < 0)
          hand.pop_back();
        else
          break;
      }
      for (auto &it : hand)
        cur += it;
      ans = max(ans, cur);
    }
  }
  co(ans);
}

// problem link: https://atcoder.jp/contests/abc128/tasks/abc128_d

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}


Fixed submission:
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define co(x) cout << x << '\n'
#define sz(x) (int)x.size()
#define all(a) a.begin(), a.end()
using namespace std;

bool cmp(int a, int b) { return (a > b); }

inline void solve() {
  int n, k, ans = 0;
  cin >> n >> k; // max(n,k) <= 100
  vector<int> a(n + 5);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i <= min(n, k); i++) {
    for (int j = 0; j <= i; j++) {
      int l = j, r = i - j, cur = 0;
      vector<int> hand;
      for (int t = 0; t < l; t++) {
        hand.pb(a[t]);
      }
      for (int t = 0; t < r; t++) {
        hand.pb(a[n - t - 1]);
      }
      sort(all(hand), cmp);
      for (int t = 1; t <= k - i and sz(hand) > 0; t++) {
        if (hand.back() < 0)
          hand.pop_back();
        else
          break;
      }
      for (auto &it : hand)
        cur += it;
      ans = max(ans, cur);
    }
  }
  co(ans);
}

// problem link: https://atcoder.jp/contests/abc128/tasks/abc128_d

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int N, K;
  cin >> N >> K;
  deque<int> V;
  for (int i = 0; i < N; i++) {
    int j;
    cin >> j;
    V.push_back(j);
  }

  int sol = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 0; j + i <= K; j++) {
      // 左からi個、右からj個取る
      deque<int> V_sub = V;
      priority_queue<int> minus;
      int sum = 0;

      for (int k = 0; k < max(i, j) && V_sub.size() > 0; k++) {
        if (k < i) {
          if (V_sub.front() < 0)
            minus.push(-V_sub.front());
          sum += V_sub.front();
          V_sub.pop_front();
        }
        if (k < j) {
          if (V_sub.back() < 0)
            minus.push(-V_sub.back());
          sum += V_sub.back();
          V_sub.pop_back();
        }
      }

      if (i + j < K && minus.size() > 0) {
        // 操作回数が余っている、かつマイナス価値がゼロでない
        for (int k = 1; i + j + k <= K && minus.size() > 0; k++) {
          sum += minus.top();
          minus.pop();
        }
      }

      sol = max(sol, sum);
    }
  }

  cout << sol << endl;
}

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

int main() {

  int N, K;
  cin >> N >> K;
  deque<int> V;
  for (int i = 0; i < N; i++) {
    int j;
    cin >> j;
    V.push_back(j);
  }

  int sol = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 0; j + i <= K; j++) {
      // 左からi個、右からj個取る
      deque<int> V_sub = V;
      priority_queue<int> minus;
      int sum = 0;

      for (int k = 0; k < max(i, j) && V_sub.size() > 0; k++) {
        if (k < i) {
          if (V_sub.front() < 0)
            minus.push(-V_sub.front());
          sum += V_sub.front();
          V_sub.pop_front();
        }
        if (V_sub.size() == 0)
          break;
        if (k < j) {
          if (V_sub.back() < 0)
            minus.push(-V_sub.back());
          sum += V_sub.back();
          V_sub.pop_back();
        }
      }

      if (i + j < K && minus.size() > 0) {
        // 操作回数が余っている、かつマイナス価値がゼロでない
        for (int k = 1; i + j + k <= K && minus.size() > 0; k++) {
          sum += minus.top();
          minus.pop();
        }
      }

      sol = max(sol, sum);
    }
  }

  cout << sol << endl;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }

  int value = -1000000000;
  for (int a = 0; a <= K; a++) {
    for (int b = 0; b <= K - a; b++) {
      vector<int> juel(0);
      int sum = 0;
      for (int i = 0; i < a; i++) {
        juel.push_back(vec.at(i));
        sum += vec.at(i);
      }
      for (int j = 0; j < b; j++) {
        juel.push_back(vec.at(N - 1 - j));
        sum += vec.at(N - 1 - j);
      }
      sort(juel.begin(), juel.end());
      int x = juel.size();
      for (int c = 0; c < min(K - a - b, x); c++) {
        if (juel.at(c) > 0) {
          break;
        } else {
          sum -= juel.at(c);
        }
      }
      // cout << a << " " << b << " " << sum << endl;
      value = max(sum, value);
    }
  }
  cout << value << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }

  int value = -1000000000;
  for (int a = 0; a <= min(K, N - 1); a++) {
    for (int b = 0; b <= min(K - a, N - a); b++) {
      vector<int> juel(0);
      int sum = 0;
      for (int i = 0; i < a; i++) {
        juel.push_back(vec.at(i));
        sum += vec.at(i);
      }
      for (int j = 0; j < b; j++) {
        juel.push_back(vec.at(N - 1 - j));
        sum += vec.at(N - 1 - j);
      }
      sort(juel.begin(), juel.end());
      int x = juel.size();
      for (int c = 0; c < min(K - a - b, x); c++) {
        if (juel.at(c) > 0) {
          break;
        } else {
          sum -= juel.at(c);
        }
      }
      // cout << a << " " << b << " " << sum << endl;
      value = max(sum, value);
    }
  }
  cout << value << endl;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int main() {
  int n, k, ans = 0;
  cin >> n >> k;
  vector<int> v;
  rep(i, n) { cin >> v[i]; }

  rep(l, k + 1) {
    rep(r, k - l + 1) {
      if (l + r > n) {
        continue;
      }
      int d = k - l - r;
      int now = 0;
      vector<int> s;
      rep(i, l) {
        now += v[i];
        s.push_back(v[i]);
      }
      for (int i = n - r; i < n; i++) {
        now += v[i];
        s.push_back(v[i]);
      }

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

      rep(i, d) {
        if (i >= s.size()) {
          break;
        }
        if (s[i] >= 0) {
          break;
        }

        now -= s[i];
      }

      ans = max(ans, now);
    }
  }

  cout << ans;

  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int main() {
  int n, k, ans = 0;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) { cin >> v[i]; }

  rep(l, k + 1) {
    rep(r, k - l + 1) {
      if (l + r > n) {
        continue;
      }
      int d = k - l - r;
      int now = 0;
      vector<int> s;
      rep(i, l) {
        now += v[i];
        s.push_back(v[i]);
      }
      for (int i = n - r; i < n; i++) {
        now += v[i];
        s.push_back(v[i]);
      }

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

      rep(i, d) {
        if (i >= s.size()) {
          break;
        }
        if (s[i] >= 0) {
          break;
        }

        now -= s[i];
      }

      ans = max(ans, now);
    }
  }

  cout << ans;

  cout << endl;
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define double long double
#define mod 1000000007
#define F first
#define S second
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 10000000000000000
#define endl '\n'
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

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

  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) cin >> v.at(i);

  int ans = -INF;
  for (int i = 0; i * 2 <= n and i <= k; i++) {
    for (int j = 0; j <= k - i; j++) {
      vector<int> tmp;
      for (int l = 0; l < j; l++)
        tmp.push_back(v.at(l));
      for (int r = 0; r < k - i - j; r++)
        tmp.push_back(v.at(n - 1 - r));
      sort(all(tmp), greater<int>());
      int cnt = 0;
      for (int o = 0; o < k - i * 2; o++)
        cnt += tmp.at(o);
      chmax(ans, cnt);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define double long double
#define mod 1000000007
#define F first
#define S second
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 10000000000000000
#define endl '\n'
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

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

  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) cin >> v.at(i);

  int ans = 0;
  for (int i = 0; i <= k; i++) {
    for (int j = 0; j <= i; j++) {
      if (j > n or 2 * j < i)
        continue;
      for (int l = 0; l <= j; l++) {
        vector<int> tmp;
        for (int o = 0; o < l; o++)
          tmp.push_back(v.at(o));
        for (int o = 0; o < j - l; o++)
          tmp.push_back(v.at(n - 1 - o));
        sort(all(tmp), greater<int>());
        int sum = 0;
        rep(o, 2 * j - i) sum += tmp.at(o);
        chmax(ans, sum);
      }
    }
  }
  cout << ans << endl;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> values(N + 1);
  vector<int> ruisekiValues(N + 2);
  vector<int> revRuisekiValues(N + 2);
  // value, index
  vector<pair<int, int>> minusValues(0);
  for (int i = 1; i <= N; i++) {
    int v;
    cin >> v;
    values[i] = v;
    if (values[i] < 0) {
      minusValues.push_back(make_pair(values[i], i));
    }
  }
  sort(minusValues.begin(), minusValues.end());
  ruisekiValues[0] = 0;
  for (int i = 1; i <= N; i++) {
    ruisekiValues[i] = values[i] + ruisekiValues[i - 1];
  }
  revRuisekiValues[N + 1] = 0;
  for (int i = N; i >= 1; i--) {
    revRuisekiValues[i] = values[i] + revRuisekiValues[i + 1];
  }
  int answer = 0;

  // int maxAddMove = min(K, N);
  for (int i = 1; i <= K; i++) {
    for (int j = 0; j <= i; j++) {
      int left = j;
      int right = N + 1 - (i - j);
      int count = ruisekiValues[left] + revRuisekiValues[right];
      int removeCount = K - i;
      for (pair<int, int> minusValue : minusValues) {
        if (removeCount == 0) {
          break;
        }
        if (minusValue.second <= left || right <= minusValue.second) {
          count -= minusValue.first;
          removeCount--;
        }
      }
      answer = max(answer, count);
    }
  }
  cout << answer << endl;
  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> values(N + 1);
  vector<int> ruisekiValues(N + 2);
  vector<int> revRuisekiValues(N + 2);
  // value, index
  vector<pair<int, int>> minusValues(0);
  for (int i = 1; i <= N; i++) {
    int v;
    cin >> v;
    values[i] = v;
    if (values[i] < 0) {
      minusValues.push_back(make_pair(values[i], i));
    }
  }
  sort(minusValues.begin(), minusValues.end());
  ruisekiValues[0] = 0;
  for (int i = 1; i <= N; i++) {
    ruisekiValues[i] = values[i] + ruisekiValues[i - 1];
  }
  revRuisekiValues[N + 1] = 0;
  for (int i = N; i >= 1; i--) {
    revRuisekiValues[i] = values[i] + revRuisekiValues[i + 1];
  }
  int answer = 0;

  int maxAddMove = min(K, N);
  for (int i = 1; i <= maxAddMove; i++) {
    for (int j = 0; j <= i; j++) {
      int left = j;
      int right = N + 1 - (i - j);
      int count = ruisekiValues[left] + revRuisekiValues[right];
      int removeCount = K - i;
      for (pair<int, int> minusValue : minusValues) {
        if (removeCount == 0) {
          break;
        }
        if (minusValue.second <= left || right <= minusValue.second) {
          count -= minusValue.first;
          removeCount--;
        }
      }
      answer = max(answer, count);
    }
  }
  cout << answer << endl;
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  long long ma = 0;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  vector<int> s(0);
  for (int a = 0; a <= min(k, n); a++) {
    for (int b = 0; a + b <= min(k, n); b++) {
      s.clear();
      for (int i = 0; i < a; i++) {
        s.push_back(v[i]);
      }
      for (int j = 0; j < b; j++) {
        s.push_back(v[n - 1 - j]);
      }
      sort(s.begin(), s.end());
      int x = 0;
      while (s[x] < 0 && (a + b + x) < k && x < s.size()) {
        s[x] = 0;
        x++;
      }
      long long sum = 0;
      for (int l = 0; l < s.size(); l++) {
        sum += (long long)s[l];
      }
      ma = max(ma, sum);
    }
  }
  cout << ma << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  long long ma = 0;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  vector<int> s(0);
  for (int a = 0; a <= min(k, n); a++) {
    for (int b = 0; a + b <= min(k, n); b++) {
      s.clear();
      for (int i = 0; i < a; i++) {
        s.push_back(v[i]);
      }
      for (int j = 0; j < b; j++) {
        s.push_back(v[n - 1 - j]);
      }
      sort(s.begin(), s.end());
      int x = 0;
      while ((a + b + x) < k && x < s.size()) {
        if (s[x] < 0) {
          s[x] = 0;
          x++;
        } else
          break;
      }
      long long sum = 0;
      for (int l = 0; l < s.size(); l++) {
        sum += (long long)s[l];
      }
      ma = max(ma, sum);
    }
  }
  cout << ma << endl;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC target("sse4,sse4.1,sse4.2,avx,avx2,tune=native")
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int MD = (int)1e9 + 7;
typedef vector<ll> VL;
void dbg() { cerr << "\n"; }
template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}
template <class T> void amax(T &a, T b) {
  if (a > b)
    a = b;
}

int main() {
  // cin.tie(0); ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VI v(n);
  rep(i, n) cin >> v[i];
  ll ans = 0;
  rep(l, k + 1) rep(r, k - l + 1) {
    if (r + l > n)
      continue;
    VL slc;
    rep(i, l) slc.push_back(v[i]);
    rep(i, r) slc.push_back(v[n - 1 - i]);
    ll sm = accumulate(all(slc), 0LL);
    sort(all(slc));
    rep(i, min(k - l - r, sz(slc))) {
      if (slc[i] > 0)
        break;
      sm -= slc[i];
    }
    ans = max(ans, sm);
  }

  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int MD = (int)1e9 + 7;
typedef vector<ll> VL;
void dbg() { cerr << "\n"; }
template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}
template <class T> void amax(T &a, T b) {
  if (a > b)
    a = b;
}

int main() {
  // cin.tie(0); ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VI v(n);
  rep(i, n) cin >> v[i];
  ll ans = 0;
  rep(l, k + 1) rep(r, k - l + 1) {
    if (r + l > n)
      continue;
    VL slc;
    rep(i, l) slc.push_back(v[i]);
    rep(i, r) slc.push_back(v[n - 1 - i]);
    ll sm = accumulate(all(slc), 0LL);
    sort(all(slc));
    rep(i, min(k - l - r, sz(slc))) {
      if (slc[i] > 0)
        break;
      sm -= slc[i];
    }
    ans = max(ans, sm);
  }

  cout << ans << "\n";
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;

int main() {
  int n, q;
  cin >> n >> q;
  vector<ll> s(n), t(n), x(n);
  vector<tuple<ll, ll, ll>> e;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> t[i] >> x[i];
    e.push_back(make_tuple(s[i] - x[i], 1, x[i]));
    e.push_back(make_tuple(t[i] - x[i], -1, x[i]));
  }
  vector<ll> d(q);
  for (int i = 0; i < q; i++) {
    cin >> d[i];
  }
  sort(begin(e), end(e));
  set<ll> st;
  int p = 0;
  int ep = 0;
  vector<int> ans(q, -1);
  while (ep < e.size() || p < q) {
    while (ep < e.size() && get<0>(e[ep]) <= d[p]) {
      if (get<1>(e[ep]) == 1) {
        st.insert(get<2>(e[ep]));
      } else {
        st.erase(get<2>(e[ep]));
      }
      ep++;
    }
    if (!st.empty())
      ans[p] = *st.begin();
    p++;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;

int main() {
  int n, q;
  cin >> n >> q;
  vector<ll> s(n), t(n), x(n);
  vector<tuple<ll, ll, ll>> e;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> t[i] >> x[i];
    e.push_back(make_tuple(s[i] - x[i], 1, x[i]));
    e.push_back(make_tuple(t[i] - x[i], -1, x[i]));
  }
  vector<ll> d(q);
  for (int i = 0; i < q; i++) {
    cin >> d[i];
  }
  sort(begin(e), end(e));
  set<ll> st;
  int p = 0;
  int ep = 0;
  vector<int> ans(q, -1);
  while (ep < e.size() && p < q) {
    while (ep < e.size() && get<0>(e[ep]) <= d[p]) {
      if (get<1>(e[ep]) == 1) {
        st.insert(get<2>(e[ep]));
      } else {
        st.erase(get<2>(e[ep]));
      }
      ep++;
    }
    if (!st.empty())
      ans[p] = *st.begin();
    p++;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

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

  vector<tuple<int, int, int>> r;
  for (int i = 0; i < n; ++i) {
    int s, t, x;
    scanf("%d %d %d\n", &s, &t, &x);
    r.push_back(make_tuple(t - x, 0, x));
    r.push_back(make_tuple(s - x, 1, x));
  }
  sort(r.begin(), r.end());

  set<int> stops;
  int t = 0;
  for (int i = 0; i < q; ++i) {
    int d;
    cin >> d;
    while (t < r.size() && get<0>(r.at(t)) <= d) {
      if (get<1>(r.at(t)) == 0) {
        stops.erase(get<2>(r.at(t)));
      } else {
        stops.insert(get<2>(r.at(t)));
      }
      t++;
    }
    int ans = -1;
    if (!stops.empty()) {
      ans = *min_element(stops.begin(), stops.end());
    }
    printf("%d\n", ans);
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

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

  vector<tuple<int, int, int>> r;
  for (int i = 0; i < n; ++i) {
    int s, t, x;
    scanf("%d %d %d\n", &s, &t, &x);
    r.push_back(make_tuple(t - x, 0, x));
    r.push_back(make_tuple(s - x, 1, x));
  }
  sort(r.begin(), r.end());

  set<int> stops;
  int t = 0;
  for (int i = 0; i < q; ++i) {
    int d;
    cin >> d;
    while (t < r.size() && get<0>(r.at(t)) <= d) {
      if (get<1>(r.at(t)) == 0) {
        stops.erase(get<2>(r.at(t)));
      } else {
        stops.insert(get<2>(r.at(t)));
      }
      t++;
    }
    int ans = -1;
    if (!stops.empty()) {
      ans = *stops.begin();
    }
    printf("%d\n", ans);
  }

  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// #include "prettyprint.hpp"

bool comp(tuple<long, long, bool> a, tuple<long, long, bool> b) {
  if (get<0>(a) == get<0>(b)) {
    return get<2>(a) < get<2>(b); // remove -> insert
  }
  return get<0>(a) < get<0>(b);
}

int main() {
  int N, Q;
  cin >> N >> Q;

  vector<tuple<long, long, bool>> events;
  for (int i = 0; i < N; i++) {
    long S, T, X;
    cin >> S >> T >> X;
    events.emplace_back(S - X, X, true);
    events.emplace_back(T - X, X, false);
  }

  sort(events.begin(), events.end(), comp);

  set<long, less<long>> bst;
  long eventIndex = 0;

  for (int i = 0; i < Q; i++) {
    long D;
    cin >> D;

    while (get<0>(events[eventIndex]) <= D) {
      if (get<2>(events[eventIndex])) {
        bst.insert(get<1>(events[eventIndex]));
      } else {
        bst.erase(get<1>(events[eventIndex]));
      }
      eventIndex++;
    }

    if (bst.size() == 0) {
      cout << -1 << endl;
    } else {
      cout << *(bst.begin()) << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// #include "prettyprint.hpp"

bool comp(tuple<long, long, bool> a, tuple<long, long, bool> b) {
  if (get<0>(a) == get<0>(b)) {
    return get<2>(a) < get<2>(b); // remove -> insert
  }
  return get<0>(a) < get<0>(b);
}

int main() {
  int N, Q;
  cin >> N >> Q;

  vector<tuple<long, long, bool>> events;
  for (int i = 0; i < N; i++) {
    long S, T, X;
    cin >> S >> T >> X;
    events.emplace_back(S - X, X, true);
    events.emplace_back(T - X, X, false);
  }

  sort(events.begin(), events.end(), comp);

  set<long, less<long>> bst;
  long eventIndex = 0;

  for (int i = 0; i < Q; i++) {
    long D;
    cin >> D;

    while (eventIndex < events.size() && get<0>(events[eventIndex]) <= D) {
      if (get<2>(events[eventIndex])) {
        bst.insert(get<1>(events[eventIndex]));
      } else {
        bst.erase(get<1>(events[eventIndex]));
      }
      eventIndex++;
    }

    if (bst.size() == 0) {
      cout << -1 << endl;
    } else {
      cout << *(bst.begin()) << endl;
    }
  }

  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll MOD = 1e9 + 7;

vector<tuple<ll, ll, ll>> T;

int main() {
  ll N, Q;
  cin >> N >> Q;
  REP(i, N) {
    ll s, t, x;
    scanf("%lld %lld %lld", &s, &t, &x);
    ll left, right;
    //[left, right)
    left = s - x;
    right = t - x;
    T.emplace_back(x, left, right);
  }

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

  set<ll> D;
  map<ll, ll> I;
  vector<ll> A(Q, -1);
  REP(i, Q) {
    ll d;
    scanf("%lld", &d);
    D.insert(d);
    I[d] = i;
  }

  for (auto &&t : T) {
    ll x, left, right;
    x = get<0>(t);
    left = get<1>(t);
    right = get<2>(t);
    auto iter0 = lower_bound(D.begin(), D.end(), left);
    auto iter1 = lower_bound(D.begin(), D.end(), right);
    for (auto iter = iter0; iter != iter1; iter++) {
      ll val = *iter;
      A[I[val]] = x;
    }

    D.erase(iter0, iter1);
  }

  REP(i, Q) {
    ll ans = A[i];
    printf("%lld\n", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll MOD = 1e9 + 7;

vector<tuple<ll, ll, ll>> T;

int main() {
  ll N, Q;
  cin >> N >> Q;
  REP(i, N) {
    ll s, t, x;
    scanf("%lld %lld %lld", &s, &t, &x);
    ll left, right;
    //[left, right)
    left = s - x;
    right = t - x;
    T.emplace_back(x, left, right);
  }

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

  set<ll> D;
  map<ll, ll> I;
  vector<ll> A(Q, -1);
  REP(i, Q) {
    ll d;
    scanf("%lld", &d);
    D.insert(d);
    I[d] = i;
  }

  for (auto &&t : T) {
    ll x, left, right;
    x = get<0>(t);
    left = get<1>(t);
    right = get<2>(t);
    auto iter0 = D.lower_bound(left);
    auto iter1 = D.lower_bound(right);
    for (auto iter = iter0; iter != iter1; iter++) {
      ll val = *iter;
      A[I[val]] = x;
    }

    D.erase(iter0, iter1);
  }

  REP(i, Q) {
    ll ans = A[i];
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;

const LL inf = 112345678901234;

int main() {
  LL N, Q;
  in >> N >> Q;
  std::vector<LL> S(N), T(N), X(N), D(Q);
  rep(i, N) in >> S[i] >> T[i] >> X[i];
  rep(i, Q) in >> D[i];

  std::set<std::pair<std::pair<LL, LL>, LL>> events;
  std::set<LL> running;
  rep(i, N) {
    events.insert(std::make_pair(std::make_pair(S[i] - X[i], X[i]), 1));
    events.insert(std::make_pair(std::make_pair(T[i] - X[i], X[i]), 0));
  }
  rep(i, Q) {
    for (auto it : events) {
      if (it.first.first > D[i])
        break;
      if (it.second == 1)
        running.insert(it.first.second);
      else
        running.erase(running.find(it.first.second));
    }

    LL ans;
    if (running.empty())
      ans = -1;
    else
      ans = *running.begin();
    out << ans << std::endl;
  }
}


Fixed submission:
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;

const LL inf = 112345678901234;

int main() {
  LL N, Q;
  in >> N >> Q;
  std::vector<LL> S(N), T(N), X(N), D(Q);
  rep(i, N) in >> S[i] >> T[i] >> X[i];
  rep(i, Q) in >> D[i];

  std::set<std::pair<std::pair<LL, LL>, LL>> events;
  std::set<LL> running;
  rep(i, N) {
    events.insert(std::make_pair(std::make_pair(S[i] - X[i], X[i]), 1));
    events.insert(std::make_pair(std::make_pair(T[i] - X[i], X[i]), 0));
  }
  rep(i, Q) {
    if (!events.empty()) {
      while ((*events.begin()).first.first <= D[i]) {
        if ((*events.begin()).second == 1)
          running.insert((*events.begin()).first.second);
        else
          running.erase(running.find((*events.begin()).first.second));
        events.erase(events.begin());
        if (events.empty())
          break;
      }
    }

    LL ans;
    if (running.empty())
      ans = -1;
    else
      ans = *running.begin();
    out << ans << std::endl;
  }
}