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

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

using namespace std;

typedef long long ll;
typedef long double ld;

const int MAXN = 1010;
vector<int> g[MAXN];
int p[MAXN];
bool used[MAXN];

bool dfs(int u) {
  if (used[u])
    return 0;
  used[u] = 1;
  for (int v : g[u]) {
    if (!p[v] || dfs(p[v])) {
      p[v] = u;
      return 1;
    }
  }
  return 0;
}

signed main() {
#ifdef POSPELOV
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<pair<int, int>> arr(n);
  for (int i = 1; i <= n; i++)
    cin >> arr[i].first >> arr[i].second;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    for (int j = 1; j <= n; j++)
      if (arr[j].first < x && arr[j].second < y)
        g[i].push_back(j);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      used[j] = 0;
    dfs(i);
  }

  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += p[i] > 0;
  cout << ans;
  return 0;
}

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

using namespace std;

typedef long long ll;
typedef long double ld;

const int MAXN = 1010;
vector<int> g[MAXN];
int p[MAXN];
bool used[MAXN];

bool dfs(int u) {
  if (used[u])
    return 0;
  used[u] = 1;
  for (int v : g[u]) {
    if (!p[v] || dfs(p[v])) {
      p[v] = u;
      return 1;
    }
  }
  return 0;
}

signed main() {
#ifdef POSPELOV
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<pair<int, int>> arr(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> arr[i].first >> arr[i].second;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    for (int j = 1; j <= n; j++)
      if (arr[j].first < x && arr[j].second < y)
        g[i].push_back(j);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      used[j] = 0;
    dfs(i);
  }

  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += p[i] > 0;
  cout << ans;
  return 0;
}
Problem p03411 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 300
using namespace std;
inline void read(int &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c ^ '0');
    c = getchar();
  }
  x *= p;
}
int n;
struct node {
  int a, b;
  bool operator<(const node &ss) const { return a + b < ss.a + ss.b; }
} e[N], x[N];
int vis[N];
int to[N << 3], beg[N >> 3], nex[N << 3];
int cnt;
void add(int x, int y) {
  to[++cnt] = y;
  nex[cnt] = beg[x];
  beg[x] = cnt;
}
int match[N << 3];
bool dfs(int x) {
  for (int i = beg[x]; i; i = nex[i]) {
    int y = to[i];
    if (!vis[y]) {
      vis[y] = 1;
      if (!match[y] || dfs(match[y])) {
        match[y] = x;
        return 1;
      }
    }
  }
  return 0;
}
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  read(n);
  for (int i = 1; i <= n; i++)
    read(e[i].a), read(e[i].b);
  for (int i = 1; i <= n; i++)
    read(x[i].a), read(x[i].b);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (e[i].a < x[j].a && e[i].b < x[j].b)
        add(i, j);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    memset(vis, 0, sizeof vis);
    if (dfs(i))
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 10000
using namespace std;
inline void read(int &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c ^ '0');
    c = getchar();
  }
  x *= p;
}
int n;
struct node {
  int a, b;
  bool operator<(const node &ss) const { return a + b < ss.a + ss.b; }
} e[N], x[N];
int vis[N];
int to[N << 3], beg[N >> 3], nex[N << 3];
int cnt;
void add(int x, int y) {
  to[++cnt] = y;
  nex[cnt] = beg[x];
  beg[x] = cnt;
}
int match[N << 3];
bool dfs(int x) {
  for (int i = beg[x]; i; i = nex[i]) {
    int y = to[i];
    if (!vis[y]) {
      vis[y] = 1;
      if (!match[y] || dfs(match[y])) {
        match[y] = x;
        return 1;
      }
    }
  }
  return 0;
}
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  read(n);
  for (int i = 1; i <= n; i++)
    read(e[i].a), read(e[i].b);
  for (int i = 1; i <= n; i++)
    read(x[i].a), read(x[i].b);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (e[i].a < x[j].a && e[i].b < x[j].b)
        add(i, j);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    memset(vis, 0, sizeof vis);
    if (dfs(i))
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03411 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
#define INF (1LL << 60)
using namespace std;
typedef long long ll;

struct edge {
  ll to, cap, rev;
};

vector<edge> G[100];
bool used[100];

void add_edge(ll from, ll to, ll cap) {
  G[from].push_back((edge){to, cap, (ll)G[to].size()});
  G[to].push_back((edge){from, 0, (ll)G[from].size() - 1}); // Directed Graph
  // G[to].push_back((edge) { from, cap, (ll) G[from].size() - 1 }); //
  // Undirected Graph
}

ll dfs(ll v, ll t, ll f) {
  if (v == t)
    return f;
  used[v] = true;
  REP(i, 0, G[v].size()) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      ll d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

ll max_flow(ll s, ll t) {
  ll flow = 0;
  while (1) {
    memset(used, 0, sizeof(used));
    ll f = dfs(s, t, INF);
    if (f == 0 || f == INF)
      break;
    flow += f;
  }
  return flow;
}

int main(void) {
  ll N;
  cin >> N;
  vector<ll> A(N), B(N), C(N), D(N);
  REP(i, 0, N) cin >> A[i] >> B[i];
  REP(i, 0, N) cin >> C[i] >> D[i];

  ll s = 2 * N, t = 2 * N + 1;
  REP(i, 0, N) add_edge(s, i, 1);
  REP(i, 0, N) add_edge(i + N, t, 1);
  REP(i, 0, N)
  REP(j, 0, N) if (A[i] < C[j] && B[i] < D[j]) add_edge(i, j + N, 1);

  cout << max_flow(s, t) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
#define INF (1LL << 60)
using namespace std;
typedef long long ll;

struct edge {
  ll to, cap, rev;
};

vector<edge> G[1000];
bool used[1000];

void add_edge(ll from, ll to, ll cap) {
  G[from].push_back((edge){to, cap, (ll)G[to].size()});
  G[to].push_back((edge){from, 0, (ll)G[from].size() - 1}); // Directed Graph
  // G[to].push_back((edge) { from, cap, (ll) G[from].size() - 1 }); //
  // Undirected Graph
}

ll dfs(ll v, ll t, ll f) {
  if (v == t)
    return f;
  used[v] = true;
  REP(i, 0, G[v].size()) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      ll d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

ll max_flow(ll s, ll t) {
  ll flow = 0;
  while (1) {
    memset(used, 0, sizeof(used));
    ll f = dfs(s, t, INF);
    if (f == 0 || f == INF)
      break;
    flow += f;
  }
  return flow;
}

int main(void) {
  ll N;
  cin >> N;
  vector<ll> A(N), B(N), C(N), D(N);
  REP(i, 0, N) cin >> A[i] >> B[i];
  REP(i, 0, N) cin >> C[i] >> D[i];

  ll s = 2 * N, t = 2 * N + 1;
  REP(i, 0, N) add_edge(s, i, 1);
  REP(i, 0, N) add_edge(i + N, t, 1);
  REP(i, 0, N)
  REP(j, 0, N) if (A[i] < C[j] && B[i] < D[j]) add_edge(i, j + N, 1);

  cout << max_flow(s, t) << endl;
}
Problem p03411 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct pp {
  int x, y, flag;
};
bool cmp(const pp &a, const pp &b) {
  if (a.x != b.x)
    return a.x < b.x;
  return a.y < b.y;
}
pp a[110], b[110];
int main() {
  int n;
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i].x >> a[i].y;
      a[i].flag = 0;
    }
    for (int i = 0; i < n; i++)
      cin >> b[i].x >> b[i].y;
    sort(a, a + n, cmp);
    sort(b, b + n, cmp);
    int s = 0;
    for (int i = 0; i < n; i++) {
      int flag = 0, p = -1;
      ;
      for (int j = 0; a[j].x <= b[i].x; j++)
        if (a[j].x < b[i].x && a[j].y < b[i].y && !a[j].flag) {
          if (!flag)
            p = j;
          else if (a[j].y > a[p].y)
            p = j;
          flag = 1;
        }
      if (flag) {
        s++;
        a[p].flag = 1;
      }
    }
    cout << s << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct pp {
  int x, y, flag;
};
bool cmp(const pp &a, const pp &b) {
  if (a.x != b.x)
    return a.x < b.x;
  return a.y < b.y;
}
pp a[110], b[110];
int main() {
  int n;
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i].x >> a[i].y;
      a[i].flag = 0;
    }
    for (int i = 0; i < n; i++)
      cin >> b[i].x >> b[i].y;
    sort(a, a + n, cmp);
    sort(b, b + n, cmp);
    int s = 0;
    for (int i = 0; i < n; i++) {
      int flag = 0, p = -1;
      ;
      for (int j = 0; a[j].x <= b[i].x && j < n; j++)
        if (a[j].x < b[i].x && a[j].y < b[i].y && !a[j].flag) {
          if (!flag)
            p = j;
          else if (a[j].y > a[p].y)
            p = j;
          flag = 1;
        }
      if (flag) {
        s++;
        a[p].flag = 1;
      }
    }
    cout << s << endl;
  }
  return 0;
}
Problem p03411 (C++)

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

using namespace std;

const int N = 100;
struct Point {
  int x, y;
  int id;
};
vector<Point> points;

bool compare(Point a, Point b) {
  if (a.x == b.x) {
    return a.y < b.y;
  } else {
    return a.x < b.x;
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < (n << 1); ++i) {
    int x, y;
    cin >> x >> y;
    if (i < n) {
      points.push_back({x, y, 0});
    } else {
      points.push_back({x, y, 1});
    }
  }
  sort(points.begin(), points.end(), compare);

  int ans = 0;
  set<int> s;
  for (int i = 0; i < (n << 1); ++i) {
    if (points[i].id == 0) {
      s.insert(points[i].y);
    } else {
      if (s.size() == 0) {
        continue;
      }
      set<int>::iterator it;
      it = s.lower_bound(points[i].y);
      if (it == s.end()) {
        --it;
        s.erase(it);
        ++ans;
      } else {
        if (s.size() == 1) {
          continue;
        } else {
          --it;
          s.erase(it);
          ++ans;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

const int N = 100;
struct Point {
  int x, y;
  int id;
};
vector<Point> points;

bool compare(Point a, Point b) {
  if (a.x == b.x) {
    return a.y < b.y;
  } else {
    return a.x < b.x;
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < (n << 1); ++i) {
    int x, y;
    cin >> x >> y;
    if (i < n) {
      points.push_back({x, y, 0});
    } else {
      points.push_back({x, y, 1});
    }
  }
  sort(points.begin(), points.end(), compare);

  int ans = 0;
  set<int> s;
  for (int i = 0; i < (n << 1); ++i) {
    if (points[i].id == 0) {
      s.insert(points[i].y);
    } else {
      if (s.size() == 0) {
        continue;
      }
      set<int>::iterator it;
      it = s.lower_bound(points[i].y);
      if (it == s.end()) {
        --it;
        s.erase(it);
        ++ans;
      } else {
        if (it == s.begin()) {
          continue;
        } else {
          --it;
          s.erase(it);
          ++ans;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03411 (C++)

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

using namespace std;

bool dfs(int v, vector<vector<int>> &graph, vector<bool> &used,
         vector<int> &match) {
  used[v] = true;
  for (int i = 0; i < graph[i].size(); ++i) {
    int u = graph[v][i];
    int w = match[u];
    if (w == -1 || (!used[w] && dfs(w, graph, used, match))) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int main(void) {
  int N;
  cin >> N;
  vector<vector<int>> graph(2 * N, vector<int>(0, 0));
  vector<pair<int, int>> P;
  vector<pair<int, int>> Q;
  for (int i = 0; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    P.push_back(make_pair(a, b));
  }
  for (int j = 0; j < N; ++j) {
    int c, d;
    cin >> c >> d;
    Q.push_back(make_pair(c, d));
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (P[i].first < Q[j].first && P[i].second < Q[j].second) {
        graph[i].push_back(N + j);
        graph[N + j].push_back(i);
      }
    }
  }
  int result = 0;
  vector<int> match(2 * N, -1);
  for (int i = 0; i < 2 * N; ++i) {
    if (match[i] == -1) {
      vector<bool> used(2 * N, false);
      if (dfs(i, graph, used, match)) {
        ++result;
      }
    }
  }
  cout << result << '\n';
  return 0;
}


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

using namespace std;

bool dfs(int v, vector<vector<int>> &graph, vector<bool> &used,
         vector<int> &match) {
  used[v] = true;
  for (int i = 0; i < graph[v].size(); ++i) {
    int u = graph[v][i];
    int w = match[u];
    if (w == -1 || (!used[w] && dfs(w, graph, used, match))) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int main(void) {
  int N;
  cin >> N;
  vector<vector<int>> graph(2 * N, vector<int>(0, 0));
  vector<pair<int, int>> P;
  vector<pair<int, int>> Q;
  for (int i = 0; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    P.push_back(make_pair(a, b));
  }
  for (int j = 0; j < N; ++j) {
    int c, d;
    cin >> c >> d;
    Q.push_back(make_pair(c, d));
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (P[i].first < Q[j].first && P[i].second < Q[j].second) {
        graph[i].push_back(N + j);
        graph[N + j].push_back(i);
      }
    }
  }
  int result = 0;
  vector<int> match(2 * N, -1);
  for (int i = 0; i < 2 * N; ++i) {
    if (match[i] == -1) {
      vector<bool> used(2 * N, false);
      if (dfs(i, graph, used, match)) {
        ++result;
      }
    }
  }
  cout << result << '\n';
  return 0;
}
Problem p03411 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <set>
using namespace std;

set<int> S;
pair<int, int> a[111];

int main(void) {
  int n, i, ans = 0;

  scanf("%d", &n);
  for (i = 0; i < 2 * n; i++) {
    scanf("%d %d", &a[i].first, &a[i].second);
    a[i].first = a[i].first * 10 + (i >= n);
  }

  sort(a, a + 2 * n);

  for (i = 0; i < 2 * n; i++)
    if (a[i].first % 10 == 0)
      S.insert(a[i].second);
    else {
      set<int>::iterator it = lower_bound(S.begin(), S.end(), a[i].second);
      if (it == S.begin())
        continue;

      it--;
      S.erase(it);
      ++ans;
    }

  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <set>
using namespace std;

set<int> S;
pair<int, int> a[222];

int main(void) {
  int n, i, ans = 0;

  scanf("%d", &n);
  for (i = 0; i < 2 * n; i++) {
    scanf("%d %d", &a[i].first, &a[i].second);
    a[i].first = a[i].first * 10 + (i >= n);
  }

  sort(a, a + 2 * n);

  for (i = 0; i < 2 * n; i++)
    if (a[i].first % 10 == 0)
      S.insert(a[i].second);
    else {
      set<int>::iterator it = lower_bound(S.begin(), S.end(), a[i].second);
      if (it == S.begin())
        continue;

      it--;
      S.erase(it);
      ++ans;
    }

  printf("%d", ans);
  return 0;
}
Problem p03411 (C++)

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

int main() {
  int n, a, b, m[105], h[105];
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    m[a] = b;
    h[a] = 1;
  }
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    m[a] = b;
    h[a] = 2;
  }
  set<int> s;
  int jwb = 0;
  for (int i = 0; i < 2 * n; ++i) {
    if (h[i] == 1)
      s.insert(m[i]);
    else {
      auto it = s.upper_bound(m[i]);
      if (it == s.begin())
        continue;
      --it;
      ++jwb;
      s.erase(it);
    }
  }
  cout << jwb << endl;
  return 0;
}

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

int main() {
  int n, a, b, m[205], h[205];
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    m[a] = b;
    h[a] = 1;
  }
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    m[a] = b;
    h[a] = 2;
  }
  set<int> s;
  int jwb = 0;
  for (int i = 0; i < 2 * n; ++i) {
    if (h[i] == 1)
      s.insert(m[i]);
    else {
      auto it = s.upper_bound(m[i]);
      if (it == s.begin())
        continue;
      --it;
      ++jwb;
      s.erase(it);
    }
  }
  cout << jwb << endl;
  return 0;
}
Problem p03411 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#define MAXN (500 + 5)
#define pii pair<int, int>
using namespace std;
struct node {
  pii poi;
  bool isB;
  bool operator<(const node x) const { return poi < x.poi; }
} a[MAXN];
int n, ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].poi.first, &a[i].poi.second), a[i].isB = false;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[n + i].poi.first, &a[n + i].poi.second),
        a[n + i].isB = true;
  sort(a + 1, a + (n << 1) + 1);
  set<int> qwq;
  for (int i = 1; i <= (n << 1); i++) {
    if (!a[i].isB)
      qwq.insert(a[i].poi.second);
    else if (!qwq.empty()) {
      set<int>::iterator x = qwq.upper_bound(a[i].poi.second);
      --x;
      ++ans;
      qwq.erase(x);
    }
  }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#define MAXN (500 + 5)
#define pii pair<int, int>
using namespace std;
struct node {
  pii poi;
  bool isB;
  bool operator<(const node x) const { return poi < x.poi; }
} a[MAXN];
int n, ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].poi.first, &a[i].poi.second), a[i].isB = false;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[n + i].poi.first, &a[n + i].poi.second),
        a[n + i].isB = true;
  sort(a + 1, a + (n << 1) + 1);
  set<int> qwq;
  for (int i = 1; i <= (n << 1); i++) {
    if (!a[i].isB)
      qwq.insert(a[i].poi.second);
    else if (!qwq.empty()) {
      set<int>::iterator x = qwq.upper_bound(a[i].poi.second);
      if (x == qwq.begin())
        continue;
      --x;
      ++ans;
      qwq.erase(x);
    }
  }
  printf("%d", ans);
  return 0;
}
Problem p03411 (C++)

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

using namespace std;

int n;
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
pii A[105];
int main() {
  //	freopen("input.txt", "r", stdin);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i].first.first >> A[i].first.second;
    A[i].second = 0;
  }
  for (int i = n; i < 2 * n; i++) {
    cin >> A[i].first.first >> A[i].first.second;
    A[i].second = 1;
  }
  sort(A, A + 2 * n);
  set<int> guys;
  int ans = 0;
  for (int i = 0; i < 2 * n; i++) {
    //		printf("%d %d %d\n", A[i].first.first, A[i].first.second,
    //A[i].second);
    if (A[i].second == 1) {
      // blue
      auto guy = guys.upper_bound(A[i].first.second);
      if (guy == guys.begin())
        continue;
      guy = prev(guy);
      //			printf("removing %d\n", *guy);
      guys.erase(guy);
      ans++;
      //			printf("%d blue\n", A[i].first.second);
    } else {
      guys.insert(A[i].first.second);
    }
  }
  cout << ans;
}

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

using namespace std;

int n;
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
pii A[205];
int main() {
  //	freopen("input.txt", "r", stdin);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i].first.first >> A[i].first.second;
    A[i].second = 0;
  }
  for (int i = n; i < 2 * n; i++) {
    cin >> A[i].first.first >> A[i].first.second;
    A[i].second = 1;
  }
  sort(A, A + 2 * n);
  set<int> guys;
  int ans = 0;
  for (int i = 0; i < 2 * n; i++) {
    //		printf("%d %d %d\n", A[i].first.first, A[i].first.second,
    //A[i].second);
    if (A[i].second == 1) {
      // blue
      auto guy = guys.upper_bound(A[i].first.second);
      if (guy == guys.begin())
        continue;
      guy = prev(guy);
      //			printf("removing %d\n", *guy);
      guys.erase(guy);
      ans++;
      //			printf("%d blue\n", A[i].first.second);
    } else {
      guys.insert(A[i].first.second);
    }
  }
  cout << ans;
}
Problem p03411 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> red(n);
  vector<pair<int, int>> blue(n);
  for (int i = 0; i < n * 2; i++) {
    cin >> red[i].first >> red[i].second;
    if (i >= n)
      cin >> blue[i].first >> blue[i].second;
  }

  sort(red.begin(), red.end());
  sort(blue.begin(), blue.end());
  vector<bool> used(n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    const int c = blue[i].first;
    const int d = blue[i].second;

    int max_y = -1;
    int min_j = -1;
    for (int j = 0; j < n; j++) {
      const int a = red[j].first;
      const int b = red[j].second;
      if (c < a || d < b) {
        continue;
      }
      if (max_y < b && !used[j]) {
        max_y = b;
        min_j = j;
      }
    }
    if (min_j >= 0) {
      used[min_j] = true;
      ++ans;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> red(n);
  vector<pair<int, int>> blue(n);
  for (int i = 0; i < n * 2; i++) {
    if (i >= n) {
      cin >> blue[i - n].first >> blue[i - n].second;
    } else {
      cin >> red[i].first >> red[i].second;
    }
  }

  sort(red.begin(), red.end());
  sort(blue.begin(), blue.end());
  vector<bool> used(n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    const int c = blue[i].first;
    const int d = blue[i].second;

    int max_y = -1;
    int min_j = -1;
    for (int j = 0; j < n; j++) {
      const int a = red[j].first;
      const int b = red[j].second;
      if (c < a || d < b) {
        continue;
      }
      if (max_y < b && !used[j]) {
        max_y = b;
        min_j = j;
      }
    }
    if (min_j >= 0) {
      used[min_j] = true;
      ++ans;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03411 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
int a[105], b[105];
int c[105], d[105];
struct edge {
  int y;
  edge *next;
};
edge *new_edge() {
  static edge a[100005];
  static int top = 0;
  return &a[top++];
}
edge *li[105];
void insert_edge(int x, int y) {
  edge *t = new_edge();
  t->y = y;
  t->next = li[x];
  li[x] = t;
}
int g[105][105];
int n;
int t[105];
bool vis[105];
bool dfs(int x, int y) {
  vis[x] = true;
  int j;
  for (j = y; j < n; j++) {
    if (g[x][j]) {
      if ((t[j] == -1) || (dfs(t[j], 0))) {
        t[j] = x;
        return true;
      }
    }
  }
  return false;
}
int main() {
#ifdef absi2011
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  scanf("%d", &n);
  int i;
  for (i = 0; i < n; i++) {
    scanf("%d%d", &a[i], &b[i]);
  }
  for (i = 0; i < n; i++) {
    scanf("%d%d", &c[i], &d[i]);
  }
  for (i = 0; i < n; i++) {
    int j;
    for (j = 0; j < n; j++) {
      if ((a[i] < c[j]) && (b[i] < d[j])) {
        g[i][j] = true;
      }
    }
  }
  memset(t, -1, sizeof(t));
  int ans = 0;
  for (i = 0; i < n; i++) {
    memset(vis, 0, sizeof(vis));
    ans += dfs(i, 0);
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
int a[105], b[105];
int c[105], d[105];
struct edge {
  int y;
  edge *next;
};
edge *new_edge() {
  static edge a[100005];
  static int top = 0;
  return &a[top++];
}
edge *li[105];
void insert_edge(int x, int y) {
  edge *t = new_edge();
  t->y = y;
  t->next = li[x];
  li[x] = t;
}
int g[105][105];
int n;
int t[105];
bool vis[105];
bool dfs(int x, int y) {
  if (vis[x])
    return false;
  vis[x] = true;
  int j;
  for (j = y; j < n; j++) {
    if (g[x][j]) {
      if ((t[j] == -1) || (dfs(t[j], 0))) {
        t[j] = x;
        return true;
      }
    }
  }
  return false;
}
int main() {
#ifdef absi2011
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  scanf("%d", &n);
  int i;
  for (i = 0; i < n; i++) {
    scanf("%d%d", &a[i], &b[i]);
  }
  for (i = 0; i < n; i++) {
    scanf("%d%d", &c[i], &d[i]);
  }
  for (i = 0; i < n; i++) {
    int j;
    for (j = 0; j < n; j++) {
      if ((a[i] < c[j]) && (b[i] < d[j])) {
        g[i][j] = true;
      }
    }
  }
  memset(t, -1, sizeof(t));
  int ans = 0;
  for (i = 0; i < n; i++) {
    memset(vis, 0, sizeof(vis));
    ans += dfs(i, 0);
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03411 (C++)

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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define Task ""

using namespace std;
typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;

struct T {
  int x, y, color;
  bool operator<(const T &other) const { return x < other.x; }
} p[maxn];
int N;

signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("A.INP", "r")) {
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  if (fopen(Task ".INP", "r")) {
    freopen(Task ".INP", "r", stdin);
    freopen(Task ".OUT", "w", stdout);
  }
  cin >> N;
  for (int i = 1; i <= 2 * N; ++i) {
    cin >> p[i].x >> p[i].y;
    if (i <= N)
      p[i].color = 1;
    else
      p[i].color = 2;
  }
  sort(p + 1, p + 1 + 2 * N);
  multiset<int> ms;
  int ans = 0;
  for (int i = 1; i <= 2 * N; ++i) {
    int first = i;
    int last = i;
    while (last <= N && p[last].x == p[first].x)
      ++last;
    --last;
    i = last;
    for (int j = first; j <= last; ++j) {
      if (p[j].color == 2) {
        if (ms.empty())
          continue;
        auto it = ms.lower_bound(p[j].y);
        if (it == ms.begin())
          continue;
        --it;
        ms.erase(it);
        ++ans;
      }
    }
    for (int j = first; j <= last; ++j) {
      if (p[j].color == 1) {
        ms.insert(p[j].y);
      }
    }
  }
  cout << ans << '\n';
}


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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define Task ""

using namespace std;
typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;

struct T {
  int x, y, color;
  bool operator<(const T &other) const { return x < other.x; }
} p[maxn];
int N;

signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("A.INP", "r")) {
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  if (fopen(Task ".INP", "r")) {
    freopen(Task ".INP", "r", stdin);
    freopen(Task ".OUT", "w", stdout);
  }
  cin >> N;
  for (int i = 1; i <= 2 * N; ++i) {
    cin >> p[i].x >> p[i].y;
    if (i <= N)
      p[i].color = 1;
    else
      p[i].color = 2;
  }
  sort(p + 1, p + 1 + 2 * N);
  multiset<int> ms;
  int ans = 0;
  for (int i = 1; i <= 2 * N; ++i) {
    int first = i;
    int last = i;
    while (last <= 2 * N && p[last].x == p[first].x)
      ++last;
    --last;
    i = last;
    for (int j = first; j <= last; ++j) {
      if (p[j].color == 2) {
        if (ms.empty())
          continue;
        auto it = ms.lower_bound(p[j].y);
        if (it == ms.begin())
          continue;
        --it;
        ms.erase(it);
        ++ans;
      }
    }
    for (int j = first; j <= last; ++j) {
      if (p[j].color == 1) {
        ms.insert(p[j].y);
      }
    }
  }
  cout << ans << '\n';
}
Problem p03411 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int girl[10010];
struct node {
  int x, y;
} arr1[100010], arr2[100010];
int mapp[10001][10001];
int vis[100010];
int n;
void solve() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (arr1[i].x < arr2[j].x && arr1[i].y < arr2[j].y)
        mapp[i][j] = 1;
    }
  }
}
bool findd(int x) {
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0 && mapp[x][i]) {
      vis[i] = 1;
      if (girl[i] == 0 || findd(girl[i])) {
        girl[i] = x;
        return true;
      }
    }
  }
  return false;
}
int main() {
  while (cin >> n) {
    int ans = 0;
    memset(mapp, 0, sizeof(mapp));
    memset(girl, 0, sizeof(girl));
    for (int i = 1; i <= n; i++) {
      cin >> arr1[i].x >> arr1[i].y;
    }
    for (int i = 1; i <= n; i++) {
      cin >> arr2[i].x >> arr2[i].y;
    }
    solve();
    for (int i = 1; i <= n; i++) {
      memset(vis, 0, sizeof(vis));
      if (findd(i))
        ans++;
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int girl[10010];
struct node {
  int x, y;
} arr1[200], arr2[200];
int mapp[200][200];
int vis[200];
int n;
void solve() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (arr1[i].x < arr2[j].x && arr1[i].y < arr2[j].y)
        mapp[i][j] = 1;
    }
  }
}
bool findd(int x) {
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0 && mapp[x][i]) {
      vis[i] = 1;
      if (girl[i] == 0 || findd(girl[i])) {
        girl[i] = x;
        return true;
      }
    }
  }
  return false;
}
int main() {
  while (cin >> n) {
    int ans = 0;
    memset(mapp, 0, sizeof(mapp));
    memset(girl, 0, sizeof(girl));
    for (int i = 1; i <= n; i++) {
      cin >> arr1[i].x >> arr1[i].y;
    }
    for (int i = 1; i <= n; i++) {
      cin >> arr2[i].x >> arr2[i].y;
    }
    solve();
    for (int i = 1; i <= n; i++) {
      memset(vis, 0, sizeof(vis));
      if (findd(i))
        ans++;
    }
    cout << ans << endl;
  }
}
Problem p03411 (C++)

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

using ll = long long;

constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

ll V;
constexpr ll MAX_V = 100;
vector<vector<ll>> G;
ll match[MAX_V];
bool used[MAX_V];

void addEdge(ll u, ll v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(ll v) {
  used[v] = true;

  for (auto &nv : G[v]) {
    ll w = match[nv];
    if (w < 0 || (!used[w] && dfs(w))) {
      match[v] = nv;
      match[nv] = v;
      return true;
    }
  }
  return false;
}

ll bipartiteMatching() {
  ll res{0};
  memset(match, -1, sizeof match);
  REP(v, V) {
    if (match[v] < 0) {
      memset(used, 0, sizeof used);
      if (dfs(v))
        ++res;
    }
  }
  return res;
}

int main(int argc, char **argv) {
  ll N;
  cin >> N;
  V = 2 * N;
  vector<ll> a(N), b(N);
  REP(i, N) cin >> a[i] >> b[i];
  G.resize(N * 2);

  REP(i, N) {
    ll x, y;
    cin >> x >> y;
    REP(j, N) {
      if (a[j] < x && b[j] < y)
        addEdge(j, i + N);
    }
  }

  std::cout << bipartiteMatching() << std::endl;
}


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

using ll = long long;

constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

ll V;
constexpr ll MAX_V = 200;
vector<vector<ll>> G;
ll match[MAX_V];
bool used[MAX_V];

void addEdge(ll u, ll v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(ll v) {
  used[v] = true;

  for (auto &nv : G[v]) {
    ll w = match[nv];
    if (w < 0 || (!used[w] && dfs(w))) {
      match[v] = nv;
      match[nv] = v;
      return true;
    }
  }
  return false;
}

ll bipartiteMatching() {
  ll res{0};
  memset(match, -1, sizeof match);
  REP(v, V) {
    if (match[v] < 0) {
      memset(used, 0, sizeof used);
      if (dfs(v))
        ++res;
    }
  }
  return res;
}

int main(int argc, char **argv) {
  ll N;
  cin >> N;
  V = 2 * N;
  vector<ll> a(N), b(N);
  REP(i, N) cin >> a[i] >> b[i];
  G.resize(N * 2);

  REP(i, N) {
    ll x, y;
    cin >> x >> y;
    REP(j, N) {
      if (a[j] < x && b[j] < y)
        addEdge(j, i + N);
    }
  }

  std::cout << bipartiteMatching() << std::endl;
}
Problem p03413 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int main() {
  int N;
  cin >> N;
  vector<Int> A(N);
  for (auto &a : A)
    cin >> a;
  vector<Int> dp(N);
  for (int i = 0; i < N; i++) {
    if (i - 2 >= 0) {
      dp[i] = max({dp[i - 2], dp[i - 2] + A[i], A[i]});
    } else {
      dp[i] = A[i];
    }
  }
  int k = max_element(begin(dp), end(dp)) - begin(dp);
  vector<bool> used(N);
  for (; k >= 0; k -= 2) {
    if (dp[k] == A[k]) {
      used[k] = true;
      break;
    } else if (k >= 2 && dp[k] == A[k] + dp[k - 2])
      used[k] = true;
    else
      assert(0);
  }
  int curr = 0;
  vector<int> ans;
  while (used[curr] == 0)
    curr++, ans.push_back(1);
  while (curr < N) {
    assert(used[curr] == 1);
    int n = 0;
    while (curr + n + 1 < N && used[curr + n + 1] == 0)
      n++;
    curr += n + 1;
    while (n >= 3)
      n -= 2, ans.push_back(3);
    if (n == 2)
      ans.push_back(3), ans.push_back(2);
    if (n == 1)
      ans.push_back(2);
  }
  cout << *max_element(begin(dp), end(dp)) << '\n';
  cout << ans.size() << '\n';
  for (auto a : ans)
    cout << a << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int main() {
  int N;
  cin >> N;
  vector<Int> A(N);
  for (auto &a : A)
    cin >> a;
  vector<Int> dp(N);
  for (int i = 0; i < N; i++) {
    if (i - 2 >= 0) {
      dp[i] = max({dp[i - 2], dp[i - 2] + A[i], A[i]});
    } else {
      dp[i] = A[i];
    }
  }
  int k = max_element(begin(dp), end(dp)) - begin(dp);
  vector<bool> used(N);
  for (; k >= 0; k -= 2) {
    if (dp[k] == A[k]) {
      used[k] = true;
      break;
    } else if (k >= 2 && dp[k] == A[k] + dp[k - 2])
      used[k] = true;
  }
  int curr = 0;
  vector<int> ans;
  while (used[curr] == 0)
    curr++, ans.push_back(1);
  while (curr < N) {
    assert(used[curr] == 1);
    int n = 0;
    while (curr + n + 1 < N && used[curr + n + 1] == 0)
      n++;
    curr += n + 1;
    while (n >= 3)
      n -= 2, ans.push_back(3);
    if (n == 2)
      ans.push_back(3), ans.push_back(2);
    if (n == 1)
      ans.push_back(2);
  }
  cout << *max_element(begin(dp), end(dp)) << '\n';
  cout << ans.size() << '\n';
  for (auto a : ans)
    cout << a << '\n';
  return 0;
}
Problem p03413 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, Max, pos, tot, ans[N], a[N];
vector<int> vec;
typedef long long ll;
ll sum1, sum2;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > Max)
      Max = a[i], pos = i;
    if (i & 1)
      sum1 += (a[i] > 0) ? a[i] : 0;
    else
      sum2 += (a[i] > 0) ? a[i] : 0;
  }
  if (Max < 0) {
    printf("%d\n%d\n", Max, n - 1);
    for (int i = n; i > pos; i--)
      printf("%d\n", i);
    for (int i = 1; i < pos; i++)
      puts("1");
    return 0;
  }
  for (int i = (sum1 > sum2 ? 1 : 2); i <= n; i += 2)
    if (a[i] > 0)
      vec.push_back(i);
  printf("%lld\n", max(sum1, sum2));
  for (int i = n; i > vec[vec.size() - 1]; i--)
    ans[++tot] = i;
  for (int i = vec.size() - 1; i >= 1; i--) {
    int t = (vec[i] - vec[i - 1]) / 2 - 1;
    while (t--)
      ans[++tot] = vec[i - 1] + 2;
    ans[++tot] = vec[i - 1] + 1;
  }
  for (int i = 1; i < vec[0]; i++)
    ans[++tot] = 1;
  printf("%d\n", tot);
  for (int i = 1; i <= tot; i++)
    printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, Max, pos, tot, ans[N], a[N];
vector<int> vec;
typedef long long ll;
ll sum1, sum2;
int main() {
  scanf("%d", &n);
  Max = -1e9 - 1; // 注意max要赋值成负的！
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > Max)
      Max = a[i], pos = i;
    if (i & 1)
      sum1 += (a[i] > 0) ? a[i] : 0;
    else
      sum2 += (a[i] > 0) ? a[i] : 0;
  }
  if (Max < 0) {
    printf("%d\n%d\n", Max, n - 1);
    for (int i = n; i > pos; i--)
      printf("%d\n", i);
    for (int i = 1; i < pos; i++)
      puts("1");
    return 0;
  }
  for (int i = (sum1 > sum2 ? 1 : 2); i <= n; i += 2)
    if (a[i] > 0)
      vec.push_back(i);
  printf("%lld\n", max(sum1, sum2));
  for (int i = n; i > vec[vec.size() - 1]; i--)
    ans[++tot] = i;
  for (int i = vec.size() - 1; i >= 1; i--) {
    int t = (vec[i] - vec[i - 1]) / 2 - 1;
    while (t--)
      ans[++tot] = vec[i - 1] + 2;
    ans[++tot] = vec[i - 1] + 1;
  }
  for (int i = 1; i < vec[0]; i++)
    ans[++tot] = 1;
  printf("%d\n", tot);
  for (int i = 1; i <= tot; i++)
    printf("%d\n", ans[i]);
  return 0;
}
Problem p03413 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T> void assign(V<T> &v, int n, const T &a = T()) {
  v.assign(n, a);
}
template <class T, class... U> void assign(V<T> &v, int n, const U &...u) {
  v.resize(n);
  for (auto &&i : v)
    assign(i, u...);
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  V<lint> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  V<> e, o;
  for (int i = 0; i < n; i += 2)
    if (a[i] > 0)
      e.push_back(i);
  for (int i = 1; i < n; i += 2)
    if (a[i] > 0)
      o.push_back(i);
  lint se = accumulate(e.begin(), e.end(), 0LL,
                       [&](lint s, int i) { return s + a[i]; });
  lint so = accumulate(o.begin(), o.end(), 0LL,
                       [&](lint s, int i) { return s + a[i]; });
  cout << max(se, so) << '\n';
  V<> ii = se > so ? e : o;
  int m = ii.size();
  cout << ii[0] + (n - 1 - ii[m - 1]) + (ii[m - 1] - ii[0]) / 2 << '\n';
  for (int i = 0; i < ii[0]; i++)
    cout << 1 << '\n';
  for (int i = n - ii[0]; i > ii[m - 1] - ii[0] + 1; i--)
    cout << i << '\n';
  for (int i = m - 2; i >= 0; i--) {
    for (int j = (ii[i] + ii[i + 1]) / 2 - ii[0] + 1; j > ii[i] - ii[0] + 1;
         j--)
      cout << j << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T> void assign(V<T> &v, int n, const T &a = T()) {
  v.assign(n, a);
}
template <class T, class... U> void assign(V<T> &v, int n, const U &...u) {
  v.resize(n);
  for (auto &&i : v)
    assign(i, u...);
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  V<lint> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  V<> e, o;
  for (int i = 0; i < n; i += 2)
    if (a[i] > 0)
      e.push_back(i);
  for (int i = 1; i < n; i += 2)
    if (a[i] > 0)
      o.push_back(i);
  if (e.size() == 0 and o.size() == 0) {
    auto itr = max_element(a.begin(), a.end());
    cout << *itr << '\n';
    cout << n - 1 << '\n';
    for (int i = 0; i < itr - a.begin(); i++)
      cout << 1 << '\n';
    for (int i = 0; i < a.end() - itr - 1; i++)
      cout << a.end() - itr - i << '\n';
    return 0;
  }
  lint se = accumulate(e.begin(), e.end(), 0LL,
                       [&](lint s, int i) { return s + a[i]; });
  lint so = accumulate(o.begin(), o.end(), 0LL,
                       [&](lint s, int i) { return s + a[i]; });
  cout << max(se, so) << '\n';
  V<> ii = se > so ? e : o;
  int m = ii.size();
  cout << ii[0] + (n - 1 - ii[m - 1]) + (ii[m - 1] - ii[0]) / 2 << '\n';
  for (int i = 0; i < ii[0]; i++)
    cout << 1 << '\n';
  for (int i = n - ii[0]; i > ii[m - 1] - ii[0] + 1; i--)
    cout << i << '\n';
  for (int i = m - 2; i >= 0; i--) {
    for (int j = (ii[i] + ii[i + 1]) / 2 - ii[0] + 1; j > ii[i] - ii[0] + 1;
         j--)
      cout << j << '\n';
  }
}
Problem p03413 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
// INSERT ABOVE HERE
signed main() {
  assert(0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
// INSERT ABOVE HERE
signed main() {
  Int n;
  cin >> n;
  deque<Int> d(n);
  for (Int i = 0; i < n; i++)
    cin >> d[i];
  vector<Int> as;
  auto a = d;
  Int ans = 0;
  if (*max_element(d.begin(), d.end()) <= 0) {
    Int k = max_element(d.begin(), d.end()) - d.begin() + 1;
    ans = d[k - 1];
    for (Int i = n; i > k; i--)
      as.emplace_back(i);
    for (Int i = 1; i < k; i++)
      as.emplace_back(1);
  } else {
    Int a = 0, b = 0;
    for (Int i = 0; i < n; i++) {
      if (i & 1)
        b += max(d[i], Int(0));
      else
        a += max(d[i], Int(0));
    }
    auto calc = [&](Int x) {
      as.emplace_back(x);
      if (x == 1)
        d.pop_front();
      else if (x == (Int)d.size())
        d.pop_back();
      else {
        d[x - 1] = d[x - 2] + d[x];
        d.erase(d.begin() + x);
        d.erase(d.begin() + x - 2);
      }
    };
    if (a < b)
      calc(1);
    while (d.size() > 2u) {
      if (d[0] < 0) {
        calc(1);
        calc(1);
      } else if (d[2] < 0) {
        calc(3);
      } else {
        calc(2);
      }
    }
    if (d.size() == 2u)
      calc(2);
    ans = d[0];
  }
  cout << ans << endl;
  cout << as.size() << endl;
  for (Int x : as)
    cout << x << endl;
  for (Int x : as) {
    if (x == 1)
      a.pop_front();
    else if (x == (Int)a.size())
      a.pop_back();
    else {
      a[x - 1] = a[x - 2] + a[x];
      a.erase(a.begin() + x);
      a.erase(a.begin() + x - 2);
    }
  }
  assert(a.size() == 1u);
  assert(a[0] == ans);
  return 0;
}
Problem p03413 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

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

const int L = 1010;
LL a[L];
LL dp[L];
int t[L];
int N;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  LL r = a[0] - 1;
  LL p = 0;
  for (int i = N - 1; i >= 0; i--) {
    dp[i] = a[i];
    for (int k = 2; i + k < N; k += 2) {
      if (dp[i] < a[i] + dp[i + k]) {
        dp[i] = a[i] + dp[i + k];
        t[i] = k;
      }
    }
    // cerr << dp[i] << ", " << t[i] << endl;
    if (r < dp[i]) {
      r = dp[i];
      p = i;
    }
  }
  // cerr << p << endl;
  vector<int> ret;
  for (int i = 0; i < p; i++) {
    ret.push_back(1);
  }
  while (t[p]) {
    for (int i = t[p] / 2; i >= 1; i--) {
      ret.push_back(i + 1);
    }
    p = p + t[p];
  }
  while (N - 1 - p) {
    ret.push_back(N - p);
  }
  cout << r << endl;
  cout << ret.size() << endl;
  for (auto v : ret) {
    cout << v << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

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

const int L = 1010;
LL a[L];
LL dp[L];
int t[L];
int N;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  LL r = a[0] - 1;
  LL p = 0;
  for (int i = N - 1; i >= 0; i--) {
    dp[i] = a[i];
    for (int k = 2; i + k < N; k += 2) {
      if (dp[i] < a[i] + dp[i + k]) {
        dp[i] = a[i] + dp[i + k];
        t[i] = k;
      }
    }
    // cerr << dp[i] << ", " << t[i] << endl;
    if (r < dp[i]) {
      r = dp[i];
      p = i;
    }
  }
  // cerr << p << endl;
  vector<int> ret;
  for (int i = 0; i < p; i++) {
    ret.push_back(1);
  }
  while (t[p]) {
    for (int i = t[p] / 2; i >= 1; i--) {
      ret.push_back(i + 1);
    }
    p = p + t[p];
  }
  while (N - 1 - p) {
    ret.push_back(N - p);
    N--;
  }
  cout << r << endl;
  cout << ret.size() << endl;
  for (auto v : ret) {
    cout << v << endl;
  }
  return 0;
}
Problem p03413 (C++)

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

using namespace std;
using ll = long long;

void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int k = max_element(a.begin() + 1, a.end()) - a.begin();
  ll res;
  int l, r;
  if (a[k] <= 0)
    res = a[k], l = k, r = k;
  else {
    ll s1 = 0;
    int l1 = 0, r1 = 0;
    for (int i = 1; i <= n; i += 2) {
      if (!l1 && a[i] > 0)
        l1 = i;
      if (a[i] > 0)
        r1 = i;
      if (a[i] > 0)
        s1 += a[i];
    }
    ll s2 = 0;
    int l2 = 0, r2 = 0;
    for (int i = 2; i <= n; i += 2) {
      if (!l2 && a[i] > 0)
        l2 = i;
      if (a[i] > 0)
        r2 = i;
      if (a[i] > 0)
        s2 += a[i];
    }
    if (s1 > s2)
      res = s1, l = l1, r = r1;
    else
      res = s2, l = l2, r = r2;
  }
  cout << res << "\n";
  vector<int> pos;
  for (int i = n; i > r; i--) {
    pos.emplace_back(i);
  }
  while (l != r) {
    if (a[r - 2] > 0) {
      pos.emplace_back(r - 1);
    } else {
      pos.emplace_back(r - 2);
    }
    r = r - 2;
  }
  for (int i = 1; i < l; i--) {
    pos.emplace_back(1);
  }
  cout << pos.size() << "\n";
  for (auto &x : pos) {
    cout << x << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}


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

using namespace std;
using ll = long long;

void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int k = max_element(a.begin() + 1, a.end()) - a.begin();
  ll res;
  int l, r;
  if (a[k] <= 0)
    res = a[k], l = k, r = k;
  else {
    ll s1 = 0;
    int l1 = 0, r1 = 0;
    for (int i = 1; i <= n; i += 2) {
      if (!l1 && a[i] > 0)
        l1 = i;
      if (a[i] > 0)
        r1 = i;
      if (a[i] > 0)
        s1 += a[i];
    }
    ll s2 = 0;
    int l2 = 0, r2 = 0;
    for (int i = 2; i <= n; i += 2) {
      if (!l2 && a[i] > 0)
        l2 = i;
      if (a[i] > 0)
        r2 = i;
      if (a[i] > 0)
        s2 += a[i];
    }
    if (s1 > s2)
      res = s1, l = l1, r = r1;
    else
      res = s2, l = l2, r = r2;
  }
  cout << res << "\n";
  vector<int> pos;
  for (int i = n; i > r; i--) {
    pos.emplace_back(i);
  }
  while (l != r) {
    if (a[r - 2] > 0) {
      pos.emplace_back(r - 1);
    } else {
      pos.emplace_back(r - 2);
    }
    r = r - 2;
  }
  for (int i = 1; i < l; i++) {
    pos.emplace_back(1);
  }
  cout << pos.size() << "\n";
  for (auto &x : pos) {
    cout << x << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}