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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#define MAX 100000
using namespace std;
typedef long long ll;
ll ans;
void solve(int a[], int n) {
  int b[1000], t[10005], vmin, vmax = 10000;
  bool vis[1000];
  for (int i = 0; i < n; i++) {
    b[i] = a[i];
    vis[i] = false;
  }
  sort(b, b + n);
  vmin = b[0];
  for (int i = 0; i < n; i++) {
    t[b[i]] = i; // 得出移动够各个元素（值不同）应该在数组中位置
  }
  for (int i = 0; i < n; i++) {
    int cur = i, m = vmax, sum = 0, num = 0; // 查找新的环
    while (1)                                // 开始搜索下一个环
    {
      vis[cur] = true;    // 将这个元素加入当前环
      num++;              // 这个环的个数加1
      sum += a[cur];      // 环的weight加1
      m = min(a[cur], m); // 更新最小值，最终得到该环的最小值
      cur = t[a[cur]];    // 得到将要移动到的位置
      if (vis[cur])
        break; // 将要移动到的位置的元素已经在环中，环已经闭合，一个环已经找到
    }
    ans += min(
        sum + (num - 2) * m,
        sum + m +
            (num + 1) *
                vmin); // 如果已经在某个环中或者根本不需要移动，那么成本为0，不影响结果
  }
}
int main() {
  int n, a[1000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  solve(a, n);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#define MAX 100000
using namespace std;
typedef long long ll;
ll ans;
void solve(int a[], int n) {
  int b[1000], t[1000], vmin, vmax = 1000;
  bool vis[1000];
  for (int i = 0; i < n; i++) {
    b[i] = a[i];
    vis[i] = false;
  }
  sort(b, b + n);
  vmin = b[0];
  for (int i = 0; i < n; i++) {
    t[b[i]] = i; // 得出移动够各个元素（值不同）应该在数组中位置
  }
  for (int i = 0; i < n; i++) {
    int cur = i, m = vmax, sum = 0, num = 0; // 查找新的环
    while (1)                                // 开始搜索下一个环
    {
      vis[cur] = true;    // 将这个元素加入当前环
      num++;              // 这个环的个数加1
      sum += a[cur];      // 环的weight加1
      m = min(a[cur], m); // 更新最小值，最终得到该环的最小值
      cur = t[a[cur]];    // 得到将要移动到的位置
      if (vis[cur])
        break; // 将要移动到的位置的元素已经在环中，环已经闭合，一个环已经找到
    }
    ans += min(
        sum + (num - 2) * m,
        sum + m +
            (num + 1) *
                vmin); // 如果已经在某个环中或者根本不需要移动，那么成本为0，不影响结果
  }
}
int main() {
  int n, a[1000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  solve(a, n);
  cout << ans << endl;
  return 0;
}
Problem p02278 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define N 1005
#define INF (1 << 30)
typedef long long ll;
using namespace std;

int main() {
  int weight[N];
  int sorted[N];
  int idx[40005];
  int n, x = INF, ans = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", weight + i);
    sorted[i] = weight[i];
    if (x > weight[i])
      x = weight[i];
  }
  sort(sorted, sorted + n);
  for (int i = 0; i < n; ++i)
    idx[sorted[i]] = i;
  bool visited[N]{false};
  for (int i = 0; i < n; ++i) {
    if (visited[i] || i == idx[weight[i]])
      continue;
    int Min = weight[i], num = 1, sum1 = weight[i];
    int j = i;
    while (true) {
      visited[j] = true;
      j = idx[weight[j]];
      if (visited[j])
        break;
      Min = min(Min, weight[j]);
      sum1 += weight[j];
      ++num;
    }
    ans += min(Min + (num + 1) * x, (num - 2) * Min) + sum1;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define N 1005
#define INF (1 << 30)
typedef long long ll;
using namespace std;

int main() {
  int weight[N];
  int sorted[N];
  int idx[N];
  int n, x = INF, ans = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", weight + i);
    sorted[i] = weight[i];
    if (x > weight[i])
      x = weight[i];
  }
  sort(sorted, sorted + n);
  for (int i = 0; i < n; ++i)
    idx[sorted[i]] = i;
  bool visited[N]{false};
  for (int i = 0; i < n; ++i) {
    if (visited[i] || i == idx[weight[i]])
      continue;
    int Min = weight[i], num = 1, sum1 = weight[i];
    int j = i;
    while (true) {
      visited[j] = true;
      j = idx[weight[j]];
      if (visited[j])
        break;
      Min = min(Min, weight[j]);
      sum1 += weight[j];
      ++num;
    }
    ans += min(Min + (num + 1) * x, (num - 2) * Min) + sum1;
  }
  cout << ans << endl;
  return 0;
}
Problem p02278 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 1000;
static const int VMAX = 10000;

int n, A[MAX], s;
int B[MAX], T[VMAX + 1];

int solve() {
  int ans = 0;

  bool V[MAX];
  for (int i = 0; i < n; i++) {
    B[i] = A[i];
    V[i] = false;
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++)
    T[B[i]] = i;
  for (int i = 0; i < n; i++) {
    if (V[i])
      continue;
    int cur = i;
    int S = 0;
    int m = VMAX;
    int an = 0;
    do {
      V[cur] = true;
      an++;
      int v = A[cur];
      m = min(m, v);
      S += v;
      cur = T[v];
    } while (!V[cur]);
    ans += min(S + (an - 2) * m, m + S + (an + 1) * s);
  }
  return ans;
}

int main() {
  cin >> n;
  s = VMAX;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    s = min(s, A[i]);
  }
  int ans = solve();
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 1000;
static const int VMAX = 10000;

int n, A[MAX], s;
int B[MAX], T[VMAX + 1];

int solve() {
  int ans = 0;

  bool V[MAX];
  for (int i = 0; i < n; i++) {
    B[i] = A[i];
    V[i] = false;
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++)
    T[B[i]] = i;
  for (int i = 0; i < n; i++) {
    if (V[i])
      continue;
    int cur = i;
    int S = 0;
    int m = VMAX;
    int an = 0;
    do {
      V[cur] = true;
      an++;
      int v = A[cur];
      m = min(m, v);
      S += v;
      cur = T[v];
    } while (V[cur]);
    ans += min(S + (an - 2) * m, m + S + (an + 1) * S);
  }
  return ans;
}

int main() {
  cin >> n;
  s = VMAX;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    s = min(s, A[i]);
  }
  int ans = solve();
  cout << ans << endl;
}
Problem p02278 (C++) — buggy submission, verdict: Runtime Error

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

static const int MAX = 1000;
static const int VMAX = 10000;

int n, A[MAX], s;
int B[MAX], T[VMAX + 1];

int solve() {
  int ans = 0;
  bool V[MAX];
  for (int i = 0; i < n; i++) {
    B[i] = A[i];
    V[i] = false;
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++) {
    T[B[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    if (V[i]) {
      continue;
    }
    int cur = i;
    int S = 0;
    int m = VMAX;
    int an = 0;
    while (true) {
      V[cur] = true;
      an++;
      int v = A[cur];
      m = min(m, v);
      S += v;
      cur = T[v];
      if (V[cur]) {
        break;
      }
    }
    ans += min(S + (an - 2) * m, m + S + (an + 1) * s);
  }
  return ans;
}
int main() {
  cin >> n;
  s = VMAX;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    s = min(s, A[i]);
  }
  int ans = solve();
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

static const int MAX = 1000;
static const int VMAX = 10000;

int n, A[MAX], s;
int B[MAX], T[MAX + 1];

int solve() {
  int ans = 0;
  bool V[MAX];
  for (int i = 0; i < n; i++) {
    B[i] = A[i];
    V[i] = false;
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++) {
    T[B[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    if (V[i]) {
      continue;
    }
    int cur = i;
    int S = 0;
    int m = VMAX;
    int an = 0;
    while (true) {
      V[cur] = true;
      an++;
      int v = A[cur];
      m = min(m, v);
      S += v;
      cur = T[v];
      if (V[cur]) {
        break;
      }
    }
    ans += min(S + (an - 2) * m, m + S + (an + 1) * S);
  }
  return ans;
}
int main() {
  cin >> n;
  s = VMAX;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    s = min(s, A[i]);
  }
  int ans = solve();
  cout << ans << endl;
  return 0;
}
Problem p02278 (C++) — buggy submission, verdict: Runtime Error

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

int n, w[1000], s;
int a[1000], d[10001]; // destination

long solve() {
  long ret = 0;

  bool v[1000];
  for (int i = 0; i < n; ++i) {
    a[i] = w[i];
    v[i] = false;
  }
  sort(a, a + n); // a[]??????w[]???????????¢?????\\???

  //??????a[i]??????????????????????????´??????d[]?????\\??????
  for (int i = 0; i < n; ++i)
    d[a[i]] = i;

  for (int i = 0; i < n; ++i) {
    if (v[i])
      continue; //??¢???????????????????????????????????????????????§???????????????????????£?????????

    //?????????????????¢???
    int cur = i; //?????¨??????
    int sum = 0;
    int m = 10000; //???????????????????????????????°????
    int an = 0;    //?????????????????§??????

    while (1) {
      v[cur] = true; //?¨??????????????¨????
      an++;
      int z = w[cur];
      sum += z;
      m = min(m, z);
      cur = d[z]; //?¬??????????
      if (v[cur])
        break; //?????¨??????
    }

    ret += min(sum + (an - 2) * m, m + sum + (an + 1) * s);
  }
  return ret;
}

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

  s = 10000;
  for (int i = 0; i < n; ++i) {
    scanf(" %d", &w[i]);
    s = min(s, w[i]); // s???w[]?????????????°????????´?
  }

  printf("%ld\\n", solve());
}

Error: 0

Fixed submission:

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

int n, w[1000], s;
int a[1000], d[1001]; // destination

long solve() {
  long ret = 0;

  bool v[1000];
  for (int i = 0; i < n; ++i) {
    a[i] = w[i];
    v[i] = false;
  }
  sort(a, a + n); // a[]??????w[]???????????¢?????\\???

  //??????a[i]??????????????????????????´??????d[]?????\\??????
  for (int i = 0; i < n; ++i)
    d[a[i]] = i;

  for (int i = 0; i < n; ++i) {
    if (v[i])
      continue; //??¢???????????????????????????????????????????????§???????????????????????£?????????

    //?????????????????¢???
    int cur = i; //?????¨??????
    int sum = 0;
    int m = 10000; //???????????????????????????????°????
    int an = 0;    //?????????????????§??????

    while (1) {
      v[cur] = true; //?¨??????????????¨????
      an++;
      int z = w[cur];
      sum += z;
      m = min(m, z);
      cur = d[z]; //?¬??????????
      if (v[cur])
        break; //?????¨??????
    }

    ret += min(sum + (an - 2) * m, m + sum + (an + 1) * s);
  }
  return ret;
}

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

  s = 10000;
  for (int i = 0; i < n; ++i) {
    scanf(" %d", &w[i]);
    s = min(s, w[i]); // s???w[]?????????????°????????´?
  }

  printf("%ld\\n", solve());
}
Problem p02278 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 200005;

int w[1005], B[1005], L[100005], flag[1005];

int main() {
  int n, ans = 0, Min = INF;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    B[i] = w[i];
    Min = min(Min, w[i]);
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++)
    L[B[i]] = i;
  for (int i = 0; i < n; i++) {
    if (flag[i])
      continue;
    int temp = i, S = INF, m = 0, cnt = 0;
    while (!flag[temp]) {
      cnt++;
      flag[temp] = 1;
      S = min(S, w[temp]);
      m += w[temp];
      temp = L[w[temp]];
    }
    ans += min(m + (cnt - 2) * S, m + (cnt + 1) * Min + S);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 200005;

int w[1005], B[1005], L[1005], flag[1005];

int main() {
  int n, ans = 0, Min = INF;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    B[i] = w[i];
    Min = min(Min, w[i]);
  }
  sort(B, B + n);
  for (int i = 0; i < n; i++)
    L[B[i]] = i;
  for (int i = 0; i < n; i++) {
    if (flag[i])
      continue;
    int temp = i, S = INF, m = 0, cnt = 0;
    while (!flag[temp]) {
      cnt++;
      flag[temp] = 1;
      S = min(S, w[temp]);
      m += w[temp];
      temp = L[w[temp]];
    }
    ans += min(m + (cnt - 2) * S, m + (cnt + 1) * Min + S);
  }
  cout << ans << endl;
  return 0;
}
Problem p02278 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#define Lens 1000
#define Maxi 10005

using namespace std;
int N, mini;
int A[Lens], B[Lens], C[Maxi];
bool V[Lens];

int solve() {
  int total = 0;
  for (int i = 0; i < N; i++) {
    B[i] = A[i];
    V[i] = false;
  }
  sort(B, B + N);
  mini = B[0];
  for (int i = 0; i < N; i++) {
    C[B[i]] = i;
    if (B[i] == A[i])
      V[i] = true;
  }
  for (int i = 0; i < N; i++) {
    while (V[i] == false) {
      int tmp = A[i];
      int min_tmp;
      int next = C[A[i]];
      int sum = A[i];
      int n = 0;
      V[i] = true;
      min_tmp = min(A[i], A[next]);
      while (!V[next]) {
        n++;
        V[next] = true;
        sum = sum + A[next];
        if (A[next] < min_tmp)
          min_tmp = A[next];
        next = C[A[next]];
      }
      total =
          total + min(sum + (n - 1) * min_tmp, sum + min_tmp + (n + 2) * mini);
    }
  }
  return total;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int s = solve();
  cout << s << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#define Lens 1000

using namespace std;
int N, mini;
int A[Lens], B[Lens], C[Lens];
bool V[Lens];

int solve() {
  int total = 0;
  for (int i = 0; i < N; i++) {
    B[i] = A[i];
    V[i] = false;
  }
  sort(B, B + N);
  mini = B[0];
  for (int i = 0; i < N; i++) {
    C[B[i]] = i;
    if (B[i] == A[i])
      V[i] = true;
  }
  for (int i = 0; i < N; i++) {
    while (V[i] == false) {
      int tmp = A[i];
      int min_tmp;
      int next = C[A[i]];
      int sum = A[i];
      int n = 0;
      V[i] = true;
      min_tmp = min(A[i], A[next]);
      while (!V[next]) {
        n++;
        V[next] = true;
        sum = sum + A[next];
        if (A[next] < min_tmp)
          min_tmp = A[next];
        next = C[A[next]];
      }
      total =
          total + min(sum + (n - 1) * min_tmp, sum + min_tmp + (n + 2) * mini);
    }
  }
  return total;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int s = solve();
  cout << s << endl;
  return 0;
}
Problem p02278 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int N, C[1005], c = 0, t, p, cost = 0;
long long res = 0;
map<int, int> H;  // number2order
map<int, int> rH; // order2number
P nodes[1005], tmp[1005];

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", C + i);
  for (int i = 0; i < N; i++) {
    tmp[i].first = C[i];
    tmp[i].second = i;
  }
  sort(tmp, tmp + N);
  for (int i = 0; i < N; i++) {
    nodes[i].first = C[i];
    nodes[tmp[i].second].second = i;
  }
  int s = tmp[0].first;
  for (int i = 0; i < N; i++) {
    int j = nodes[i].second;
    if (j >= 0 && j != 1) {
      int n = 1, amin, sum;
      amin = sum = nodes[i].first;
      while (j != i) {
        int next = nodes[j].second;
        if (nodes[j].second < amin)
          amin = nodes[j].first;
        sum += nodes[j].first;
        n++;
        nodes[j].second = -1;
        j = next;
      }
      cost += min(sum + (n - 2) * amin, sum + amin + (n + 1) * s);
    }
  }
  printf("%d\\n", cost);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef struct D {
  int c;
  vector<P> W;
  bool operator<(const D &a) const { return c > a.c; }
} D;
int N, W[1005], C[1005], c = 0, t, p, res = 1000000000;
map<int, int> H;
map<int, int> rH;
map<vector<P>, int> MC;
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", C + i);
  memcpy(W, C, sizeof(C));
  sort(C, C + N);
  for (int i = 0; i < N; i++) {
    H[C[i]] = i;
    rH[i] = C[i];
  }
  D tmp = {0};
  for (int i = 0; i < N; i++)
    tmp.W.push_back(make_pair(W[i], H[W[i]]));
  MC[tmp.W] = 0;
  priority_queue<D> Q;
  Q.push(tmp);
  for (; !Q.empty();) {
    tmp = Q.top();
    Q.pop();
    if (MC[tmp.W] != 0 && MC[tmp.W] < tmp.c)
      continue;
    int f = 1;
    for (int i = 0; i < tmp.W.size(); i++) {
      if (tmp.W[i].second != i) {
        f = 0;
        D ins = tmp;
        ins.c += tmp.W[i].first + tmp.W[tmp.W[i].second].first;
        swap(ins.W[i], ins.W[ins.W[i].second]);
        if (MC[ins.W] == 0 || MC[ins.W] > ins.c) {
          MC[ins.W] = ins.c;
          Q.push(ins);
        }
      }
    }
    if (f) {
      res = min(res, tmp.c);
      break;
    }
  }
  printf("%d\\n", res);
  return 0;
}
Problem p02278 (C++) — buggy submission, verdict: Runtime Error

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

int n, w[1010], a[1010], b[10010] = {}, mi = 100000;
bool used[1010];

int partition(int *A, int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}

void quicksort(int *A, int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int solve() {

  int ans = 0;

  quicksort(a, 0, n - 1);
  for (int i = 0; i < n; i++)
    b[a[i]] = i;

  for (int i = 0; i < n; i++) {
    int cnt = 0, sum = 0, now = i, mimi = 100000;

    if (used[i])
      continue;

    while (true) {
      if (used[now])
        break;
      cnt++;
      used[now] = true;
      mimi = min(mimi, w[now]);
      sum += w[now];
      now = b[w[now]];
    }

    ans += min(sum + (cnt - 2) * mimi, sum + mimi + (cnt + 1) * mi);
  }

  return ans;
}

int main() {

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    a[i] = w[i];
    mi = min(mi, w[i]);
  }

  cout << solve() << endl;

  return 0;
}


Error: 0

Fixed submission:

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

int n, w[1010], a[1010], b[1010], mi = 100000;
bool used[1010];

int partition(int *A, int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}

void quicksort(int *A, int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int solve() {

  int ans = 0;

  for (int i = 0; i < n; i++)
    b[a[i]] = i;

  for (int i = 0; i < n; i++) {
    int cnt = 0, sum = 0, now = i, mimi = 100000;

    if (used[i])
      continue;

    while (true) {
      if (used[now])
        break;
      cnt++;
      used[now] = true;
      mimi = min(mimi, w[now]);
      sum += w[now];
      now = b[w[now]];
    }

    ans += min(sum + (cnt - 2) * mimi, sum + mi + (cnt + 1) * mimi);
  }

  return ans;
}

int main() {

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    a[i] = w[i];
    mi = min(mi, w[i]);
  }

  quicksort(a, 0, n - 1);

  cout << solve() << endl;

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

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;

int do_cycle(vector<int> &v, int x) {
  int s = 0, m = 1 << 30;
  for (auto &e : v)
    s += e, m = min(m, e);
  return min(s + (v.size() - 2) * m, s + m + (v.size() + 1) * x);
}

int main() {
  int N, x = 1 << 30, r = 0;
  scanf("%d", &N);
  vector<int> v(N), b(N), se(N);
  map<int, int> m;
  vector<vector<int>> cycles;
  for (int i = 0; i < N; i++)
    scanf("%d", &v[i]), x = min(x, v[i]), se[i] = v[i];
  sort(se.begin(), se.end());
  int i = 0;
  for (auto &e : se) {
    m[e] = i++;
  }
  for (int i = 0; i < N; i++)
    if (!b[i]) {
      vector<int> cycle;
      int k = i;
      do {
        cycle.push_back(se[k]);
        b[k] = 1;
        k = m[v[k]];
      } while (k != i);
      r += do_cycle(cycle, x);
    }
  printf("%d\\n", r);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <unordered_map>
using namespace std;

namespace std {
template <typename T> inline void hash_combine(size_t &seed, T const &v) {
  seed ^= hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template <typename It> inline size_t hash_range(It first, It last) {
  size_t seed = 0;
  hash_range(seed, first, last);
  return seed;
}
template <typename It> inline void hash_range(size_t &seed, It first, It last) {
  for (; first != last; ++first)
    hash_combine(seed, *first);
}
template <typename A, typename B> class hash<pair<A, B>> {
public:
  size_t operator()(pair<A, B> const &p) const {
    size_t seed = 0;
    hash_combine(seed, p.first);
    hash_combine(seed, p.second);
    return seed;
  }
};
template <typename T, size_t N> class hash<array<T, N>> {
public:
  size_t operator()(array<T, N> const &container) const {
    return hash_range(container.begin(), container.end());
  }
};
template <typename T, typename A> class hash<vector<T, A>> {
public:
  size_t operator()(vector<T, A> const &container) const {
    return hash_range(container.begin(), container.end());
  }
};
} // namespace std

int main() {
  int N;
  scanf("%d", &N);
  vector<char> v(N);
  for (int i = 0; i < N; i++)
    scanf("%d", &v[i]);
  priority_queue<pair<int, vector<char>>> q;
  unordered_map<vector<char>, int> depth;

  q.push(make_pair(0, v));
  int R;
  for (; !q.empty();) {
    auto cur = q.top();
    q.pop();
    if (depth.find(cur.second) != depth.end())
      continue;
    depth[cur.second] = cur.first;
    bool f = false;
    for (int i = 0; i < N - 1; i++)
      for (int j = i + 1; j < N; j++) {
        if (cur.second[i] > cur.second[j])
          f = true;
        swap(cur.second[i], cur.second[j]);
        if (depth.find(cur.second) == depth.end())
          q.push(
              make_pair(cur.first - cur.second[i] - cur.second[j], cur.second));
        swap(cur.second[i], cur.second[j]);
      }
    if (!f) {
      R = -cur.first;
      break;
    }
  }
  printf("%d\\n", R);
}