alexjercan/bugnet huggingface dataset:
Problem p00552 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#pragma warning(disable : 4996)
int N, Q, X[200009], D[200009], L[200009];
long long bit[2][262156];
void add(int t, int p, int x) {
  for (int i = p + 1; i <= 262144; i += i & (-i)) {
    bit[t][i] += x;
  }
}
long long sum(int t, int p) {
  long long ret = 0;
  for (int i = p; i >= 1; i -= i & (-i)) {
    ret += bit[t][i];
  }
  return ret;
}
int bs(int t, long long x) {
  if (x <= 0)
    return 0;
  int cur = 0;
  for (int i = 17; i >= 0; i--) {
    if (bit[t][cur + (1 << i)] < x) {
      cur += 1 << i;
      x -= bit[t][cur];
    }
  }
  return cur + 1;
}
int main() {
  scanf("%d %d", &N, &Q);
  for (int i = 0; i < Q; i++)
    scanf("%d %d %d", &X[i], &D[i], &L[i]);
  for (int i = 1; i < N; i++)
    add(0, i, 1), add(1, i, 1);
  long long cs = 0, ct = 0;
  for (int i = Q - 1; i >= 0; i--) {
    if (D[i] == 1) {
      int pr = bs(0, X[i] + ct) - 1;
      if (pr != -1)
        add(1, pr, 2 * L[i]);
      else
        cs -= 2 * L[i];
      cs += 2 * L[i];
    } else {
      int pr = bs(1, X[i] + cs) - 1;
      if (pr != -1)
        add(0, pr, 2 * L[i]);
      else
        ct -= 2 * L[i];
    }
  }
  for (int i = 0; i < N; i++) {
    long long px = sum(0, i + 1), py = sum(1, i + 1);
    printf("%lld\\n", (px - py + cs - ct) / 2);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cassert>
#include <iostream>
using namespace std;
int N, Q, x[3009], d[3009], l[3009];
int main() {
  cin >> N >> Q;
  assert(N <= 3000 && Q <= 3000);
  for (int i = 0; i < Q; i++)
    cin >> x[i] >> d[i] >> l[i];
  for (int i = 0; i < N; i++) {
    long long px = i, py = 0;
    for (int j = Q - 1; j >= 0; j--) {
      if (d[j] == 1 && x[j] - px - py > 0)
        px -= l[j], py += l[j];
      if (d[j] == 2 && x[j] - px + py <= 0)
        px += l[j], py += l[j];
    }
    cout << py << endl;
  }
  return 0;
}
Problem p00554 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n, m, a[1001], b[1001];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] > a[j])
        swap(a[i], a[j]);
    }
  }
  int ans = 0;
  for (int i = 0; i < m - 1; i++) {
    if (a[i] <= n)
      ans += n - a[i];
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, m, a[100], b[100];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] > a[j])
        swap(a[i], a[j]);
    }
  }
  int ans = 0;
  for (int i = 0; i < m - 1; i++) {
    if (a[i] <= n)
      ans += n - a[i];
  }
  cout << ans << endl;
}
Problem p00554 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  multiset<int> a;
  int sum = 0, co = 0;
  for (int z = 0; z < m; z++) {
    int b, c;
    cin >> b >> c;
    if (b >= n)
      co++;
    else {
      a.insert(n - b);
    }
  }
  set<int>::iterator it = a.begin();
  while (co < m - 1) {
    sum += (*it);
    it++;
    co++;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  set<int> a;
  int sum = 0, co = 0;
  for (int z = 0; z < m; z++) {
    int b, c;
    cin >> b >> c;
    if (b >= n)
      co++;
    else {
      a.insert(n - b);
    }
  }
  set<int>::iterator it;
  while (co < m - 1) {
    it = a.begin();
    sum += (*it);
    a.erase(it);
    co++;
  }
  cout << sum << endl;
}
Problem p00554 (C++) — buggy submission, verdict: Runtime Error

// q2.cpp

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 10000

int main() {
  int n, m, a, b, s[SIZE];

  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);

    s[i] = max(n - a, 0);
  }

  sort(s, s + m);

  int ans = 0;

  for (int i = 0; i < m - 1; i++) {
    ans += s[i];
  }

  cout << ans << endl;

  return 0;
}

Error: 0
s[i] = 0
s[i] = 0
s[i] = 1
s[i] = 3


Fixed submission:

// q2.cpp

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 10000

int main() {
  int n, m, a, b, s[SIZE];

  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);

    s[i] = max(n - a, 0);
  }

  sort(s, s + m);

  int ans = 0;

  for (int i = 0; i < m - 1; i++) {
    ans += s[i];
    debug(s[i]);
  }

  cout << ans << endl;

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

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

int main() {
  int N, M, cnt = 0, ans = 0;

  cin >> N >> M;

  int *a, *b;

  a = new int[M];
  b = new int[M];

  for (int i = 0; i < M; ++i) {
    cin >> a[i] >> b[i];

    if (a[i] >= N) {
      ++cnt;
    }
  }

  sort(a, a + M);
  reverse(a, a + M);

  int counter = 0;
  for (int i = cnt + 1; i <= M - 1; ++i) {
    if (a[counter] < N) {
      ans += N - a[counter];
    } else {
      --i;
    }
    ++counter;
  }

  cout << ans << endl;

  delete[] a;
  delete[] b;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N, M, cnt = 0, ans = 0;

  cin >> N >> M;

  int *a, *b, *w;

  a = new int[M];
  b = new int[M];

  for (int i = 0; i < M; ++i) {
    cin >> a[i] >> b[i];

    if (a[i] >= N) {
      ++cnt;
    }
  }

  sort(a, a + N);
  reverse(a, a + N);

  int counter = 0;
  for (int i = cnt + 1; i <= M - 1; ++i) {
    if (a[counter] < N) {
      ans += N - a[counter];
    } else {
      --i;
    }
    ++counter;
  }

  cout << ans << endl;

  delete[] a;
  delete[] b;
  return 0;
}
Problem p00555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int area[99999], N, M, D, spa, cnt = 0;
  char AREA[101][101];
  cin >> N >> M >> D;
  for (int i = 0; i < N; i++) {
    scanf("%s", &AREA[i]);
    spa = 0;
    for (int j = 0; j < M; j++) {
      if (AREA[i][j] == '.')
        spa++;
      if (spa >= D) {
        if (AREA[i][j] == '#' || j == M - 1) {
          area[cnt] = spa;
          spa = 0;
          cnt++;
        }
      }
      if (AREA[i][j] == '#')
        spa = 0;
    }
  }

  for (int i = 0; i < M; i++) {
    spa = 0;
    for (int j = 0; j < N; j++) {
      if (AREA[j][i] == '.')
        spa++;
      if (spa >= D) {
        if (AREA[j][i] == '#' || j == N - 1) {
          area[cnt] = spa;
          spa = 0;
          cnt++;
        }
      }
      if (AREA[j][i] == '#')
        spa = 0;
    }
  }

  int ans = 0;
  for (int i = 0; i < cnt; i++) {
    ans += area[i] - D + 1;
  }
  cout << ans << endl;
  return 0;
}
/*
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
int min() {
        int AREA[101][101], N, M, D;
        char line[101];
        cin >> N >> M >> D;
        for (int i = 0; i < N; i++) {
                scanf("%s", &line);
                for (int j = 0; j < M; j++) {
                        if (line[j] == '.')
                                AREA[i][j] = 0;
                        else if (line[j] == '#')
                                AREA[i][j] = 1;
                }
        }

}
*/


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n, m, d, map[101][101] = {0}, cnt = 0, ans = 0;
  cin >> n >> m >> d;
  for (int i = 0; i < n; i++) {
    char s[201];
    scanf("%s", s);
    for (int j = 0; j < m; i++) {
      if (s[j] == '#')
        map[i][j] = -1;
    }
  }
  for (int i = 0; i < n; i++) {
    cnt = 0;
    for (int j = 0; j < m; j++) {
      if (map[i][j] == 0) {
        cnt++;
        if (cnt >= d)
          ans++;
      } else
        cnt = 0;
    }
  }
  for (int i = 0; i < m; i++) {
    cnt = 0;
    for (int j = 0; j < n; j++) {
      if (map[j][i] != -1) {
        cnt++;
        if (cnt >= d)
          ans++;
      } else
        cnt = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00555 (C++) — buggy submission, verdict: Runtime Error

//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
char chr;
int N, M, D;
int A[101][101];
int main() {
  cin >> N >> M >> D;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> chr;
      if (chr == '.')
        A[i][j] = 1;
      if (chr == '#')
        A[i][j] = 0;
    }
  }
  int S = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      int R = 1;
      for (int k = 0; k < D; k++) {
        if (A[i][j + k] == 0) {
          R = 0;
          k = D;
        }
      }
      S += R;
      R = 1;
      for (int k = 0; k < D; k++) {
        if (A[i + k][j] == 0) {
          R = 0;
          k = D;
        }
      }
      S += R;
    }
  }

  cout << S << endl;
  return 0;
}

Error: 0

Fixed submission:

//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
char chr;
int N, M, D;
int A[101][101];
int main() {
  cin >> N >> M >> D;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> chr;
      if (chr == '.')
        A[i][j] = 1;
      if (chr == '#')
        A[i][j] = 0;
    }
  }
  int S = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      int R = 1;
      for (int k = 0; k < D; k++) {
        if (A[i][j + k] == 0)
          R = 0;
      }
      S += R;
      R = 1;
      for (int k = 0; k < D; k++) {
        if (A[i + k][j] == 0)
          R = 0;
      }
      S += R;
    }
  }

  cout << S << endl;
  return 0;
}
Problem p00555 (C++) — buggy submission, verdict: Runtime Error

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

bool b[256][256];

int main() {
  int n, m, d;
  cin >> n >> m >> d;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      b[i][j] = (c == '.');
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      bool f = true;
      for (int k = 0; k < d; k++) {
        f &= b[i + k][j];
      }

      ans += f;

      f = true;
      for (int k = 0; k < d; k++) {
        f &= b[i][j + k];
      }

      ans += f;
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

bool b[110][110];

int main() {
  int n, m, d;
  cin >> n >> m >> d;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      b[i][j] = (c == '.');
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      bool f = true;
      for (int k = 0; k < d; k++) {
        f &= b[i + k][j];
      }

      ans += f;

      f = true;
      for (int k = 0; k < d; k++) {
        f &= b[i][j + k];
      }

      ans += f;
    }
  }

  cout << ans << endl;
}
Problem p00555 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
char s[101][101];
int main() {
  int i, j, k, n, m, d, q, ans = 0;
  bool lyes, tyes;
  cin >> n >> m >> d;
  q = m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++)
      cin >> s[i][j];
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i][j] == '.') {
        for (k = 1; k < d; k++) {
          if (s[i][j + k] != '.')
            break;
        }
        if (k == d)
          ans++;
        for (k = 1; k < d; k++) {
          if (s[i + k][j] != '.')
            break;
        }
        if (k == d)
          ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
char s[101][101];
int main() {
  int i, j, k, n, m, d, q, ans = 0;
  bool lyes, tyes;
  cin >> n >> m >> d;
  q = m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++)
      cin >> s[i][j];
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i][j] == '.') {
        lyes = true;
        tyes = true;
        for (k = 1; k < d; k++) {
          if (s[i][j + k] != '.')
            lyes = false;
          if (s[i + k][j] != '.')
            tyes = false;
          if (lyes == false && tyes == false)
            break;
        }
        if (lyes == true)
          ans++;
        if (tyes == true)
          ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00555 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;

bool block[110][110];

int check(int x, int y, int D, int N, int M) {
  int ans = 0;
  if (x + D <= N) {
    bool flag = 1;
    FOR(i, 0, D) if (block[x + i][y]) flag = 0;
    if (flag)
      ans++;
  }
  if (y + D <= M) {
    bool flag = 1;
    FOR(i, 0, D) if (block[x][y + i]) flag = 0;
    if (flag)
      ans++;
  }
  return ans;
}

int main() {
  int N, M, D, ans = 0;
  char c;
  cin >> N >> M >> D;
  FOR(i, 0, N) FOR(j, 0, M) {
    cin >> c;
    if (c == '#')
      block[i][j] = 1;
  }
  FOR(i, 0, N) FOR(j, 0, M) ans += check(i, j, D, N, M);
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define debug(x) cerr << #x << ": " << x << endl

using LL = long long;
template <class T> using V = vector<T>;

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

  int H, W, R;
  cin >> H >> W >> R;
  V<string> vs(H);
  FOR(i, 0, H) { cin >> vs[i]; }
  auto check = [&vs](int i, int j, int R) {
    int H = SZ(vs);
    int W = SZ(vs[0]);
    int f = 1;

    FOR(k, j, j + R) { f &= vs[i][k] == '.'; }
    return f;
  };
  auto flip = [&]() {
    V<string> vs2(W, string('.', H));
    FOR(i, 0, H) {
      FOR(j, 0, W) { vs2[j][i] = vs[i][j]; }
    }
    vs = vs2;
  };
  int ans = 0;
  FOR(k, 0, 2) {
    FOR(i, 0, H) {
      FOR(j, 0, W - R + 1) { ans += check(i, j, R); }
    }
    flip();
    swap(H, W);
  }
  cout << ans << endl;

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

#include <iostream>

using namespace std;

int m, n, d, field[110][110];

void judge() {
  int ans = 0;
  /////////////////////////////////////?¨????????????????????????´???
  for (
      int i = 0; i <= n - d;
      i++) { //(????????°????¨??????????????¨???????????????????????????????????????´??????5-2+1=4???)
    for (int j = 0; j < m; j++) { // j???????????????
      bool flag = true;
      for (int k = 0; k < d; k++) {
        if (field[i + k][j] == 1)
          flag = false;
      }
      if (flag == true)
        ans += 1;
    }
  }
  /////////////////////////////////////??????????????????????????´???
  for (int i = 0; i < n; i++) {        // i???????????????
    for (int j = 0; j <= m - d; j++) { //?¨???¨????§?
      bool flag = true;
      for (int k = 0; k < d; k++) {
        if (field[i][j + k] == 1)
          flag = false;
      }
      if (flag == true)
        ans += 1;
    }
  }

  //??????
  cout << ans << endl;
}

int main() {
  cin >> n >> m >> d;

  char place;

  for (int i = 0; i < n; i++) {   //????????????
    for (int j = 0; j < m; j++) { //??±?\\???????
      cin >> place;
      if (place == '#')
        field[i][j] = 1; //?¨????i??????????????????j??????????????????
      else
        field[i][j] = 0;
    }
  }

  judge();

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int m, n, d, field[100][100];

void judge() {
  bool flag = true;
  int ans = 0;
  /////////////////////////////////////?¨????????????????????????´???
  for (
      int i = 0; i < m - d + 1;
      i++) { //(????????°????¨??????????????¨???????????????????????????????????????´??????5-2+1=4???)
    for (int j = 0; i < n; j++) { // j???????????????
      for (int k = 0; k < d; k++) {
        if (field[i + k][j] == 1)
          flag = false;
      }
      if (flag == true)
        ans += 1;

      flag = true;
    }
  }
  /////////////////////////////////////??????????????????????????´???
  for (int i = 0; i < m; i++) {           // i???????????????
    for (int j = 0; i < n - d + 1; j++) { //?¨???¨????§?
      for (int k = 0; k < d; k++) {
        if (field[i][j + k] == 1)
          flag = false;
      }
      if (flag == true)
        ans += 1;

      flag = true;
    }
  }

  //??????
  cout << ans << endl;
}

int main() {
  cin >> m >> n >> d;

  if (m < d || n < d) {
    cout << 0 << endl;
    return 0;
  }

  char place;

  for (int i = 0; i < m; i++) {   //??±?\\???????
    for (int j = 0; j < n; j++) { //????????????
      cin >> place;
      if (place == '#')
        field[i][j] = 1; //?¨????i??????????????????j??????????????????
      else
        field[i][j] = 0;
    }
  }

  judge();

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

#include <algorithm>
#include <iostream>
#include <vector>
int N, M;
int ans = 1 << 30;
int C[21][100010];
int num[21];
int NU[100010];

int DP[1 << 21][21];
int ac[1 << 21];
int main() {
  std::cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    std::cin >> NU[i];
    num[NU[i]]++;
  }
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= num[i]; j++) {
      if (NU[j] != i)
        C[i][num[i]]++;
    }
    for (int j = num[i] + 1; j <= N; j++) {
      C[i][j] = C[i][j - 1];
      if (NU[j - num[i]] != i)
        C[i][j]--;
      if (NU[j] != i)
        C[i][j]++;
    }
  }
  for (int i = 0; i < 1 << (M + 1); i++) {
    for (int j = 1; j <= M; j++) {
      if ((i >> j) & 1) {
        ac[i] += num[j];
      }
    }
  }
  for (int i = 1; i < 1 << (M + 1); i++) {
    for (int j = 1; j <= M + 1; j++) {
      DP[i][j] = 1 << 30;
    }
  }
  for (int i = 0; i < (1 << (M + 1)) - 1; i++) {
    for (int j = 1; j <= M; j++) {
      if (!(i >> j & 1))
        continue;
      DP[i][j] = DP[i ^ (1 << j)][M + 1] + C[j][ac[i]];
      DP[i][M + 1] = std::min(DP[i][M + 1], DP[i][j]);
    }
  }
  std::cout << DP[(1 << (M + 1)) - 2][M + 1] << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
int N, M;
int ans = 1 << 30;
int C[21][100010];
int num[21];
int NU[100010];

void dfs(int s, int cost, int sx) {
  if (s == (1 << (M + 1)) - 2) {
    ans = std::min(ans, cost);
    return;
  }
  for (int i = 1; i <= M; i++) {
    if (!(s >> i & 1)) {
      dfs(s | 1 << i, cost + C[i][sx + num[i]], sx + num[i]);
    }
  }
}

int main() {
  std::cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    std::cin >> NU[i];
    num[NU[i]]++;
  }
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= num[i]; j++) {
      if (NU[j] != i)
        C[i][num[i]]++;
    }
    for (int j = num[i] + 1; j <= N; j++) {
      C[i][j] = C[i][j - 1];
      if (NU[j - num[i]] != i)
        C[i][j]--;
      if (NU[j] != i)
        C[i][j]++;
    }
  }
  dfs(0, 0, 0);
  std::cout << ans << std::endl;
  return 0;
}
Problem p00556 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int search(int);
int **line;
int N, M;
int main() {
  cin >> N >> M;
  line = new int *[N + 1];
  line[0] = new int[M + 1]{0};
  int dummy;
  for (int i = 1; i <= N; ++i) {
    cin >> dummy;
    line[i] = new int[M + 1];
    for (int j = 1; j <= M; ++j) {
      line[i][j] = line[i - 1][j];
    }
    ++line[i][dummy];
  }
  int *dp;
  dp = new int[1 << M];
  dp[0] = 0;
  for (int i = 1; i < (1 << M); ++i) {
    dp[i] = N + 1;
  }
  for (int i = 0; i < (1 << M) - 1; ++i) {
    for (int j = 0; j < M; ++j) {
      if (!(i & (1 << j))) {
        int nlong = search(i);
        dp[i | (1 << j)] =
            min(dp[i | (1 << j)],
                dp[i] + line[N][j + 1] -
                    (line[nlong + line[N][j + 1]][j + 1] - line[nlong][j + 1]));
      }
    }
  }
  cout << dp[(1 << M) - 1] << endl;
  return 0;
}
int search(int use) {
  int res = 0;
  for (int i = 0; i < M; ++i) {
    if (use & (1 << i)) {
      res += line[N][i + 1];
    }
  }
  return res;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int **dp;
int **line;
int search(int, int);
int N, M;
int finish;
int main() {
  cin >> N >> M;
  line = new int *[N + 1];
  line[0] = new int[M + 1]{0};
  int dummy;
  for (int i = 1; i <= N; ++i) {
    line[i] = new int[M + 1];
    for (int j = 1; j <= M; ++j) {
      line[i][j] = line[i - 1][j];
    }
    cin >> dummy;
    ++line[i][dummy];
  }
  dp = new int *[N + 1];
  for (int i = 0; i <= N; ++i) {
    dp[i] = new int[1 << M];
    for (int j = 0; j < (1 << M); ++j) {
      dp[i][j] = -1;
    }
  }
  finish = (1 << M) - 1;
  cout << search(0, 0) << endl;
  return 0;
}
int search(int place, int use) {
  if (dp[place][use] != -1)
    return dp[place][use];
  if (use == finish) {
    return 0;
    dp[place][use] = 0;
  }
  int res = N + 1;
  for (int i = 0; i < M; ++i) {
    if (!(use & (1 << i))) {
      res = min(
          res, search(place + line[N][i + 1], use | (1 << i)) + line[N][i + 1] -
                   (line[place + line[N][i + 1]][i + 1] - line[place][i + 1]));
    }
  }
  dp[place][use] = res;
  return res;
}