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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int d, n, m;
int s[111111];
int main(void) {
  while (cin >> d && d) {
    cin >> n >> m;
    memset(s, 0, sizeof(s));
    s[0] = 0;
    s[n] = d;
    for (int i = 1; i < n; i++) {
      cin >> s[i];
    }
    sort(s, s + n + 1);

    int res = 0;
    for (int i = 0; i < m; i++) {
      int k;
      cin >> k;
      int *p = upper_bound(s, s + n + 1, k);
      res += min(*p - k, k - *(p - 1));
    }
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int d, n, m;
int s[111111];
int t[111111];
int f[111111];
int main(void) {
  while (cin >> d && d) {
    cin >> n >> m;
    memset(s, 0, sizeof(s));
    memset(t, 0, sizeof(t));
    memset(f, 0, sizeof(f));
    s[0] = 0;
    s[n] = d;
    for (int i = 1; i < n; i++) {
      cin >> s[i];
      t[s[i]] = i;
      f[s[i]] = i;
    }

    int p = 0;
    for (int i = 0; i <= d; i++) {
      if (t[i])
        p = t[i];
      t[i] = p;
    }
    p = n;
    for (int i = d; i >= 0; i--) {
      if (f[i])
        p = f[i];
      f[i] = p;
    }

    int res = 0;
    for (int i = 0; i < m; i++) {
      int k;
      cin >> k;
      res += min(k - s[t[k]], s[f[k]] - k);
    }
    cout << res << endl;
  }
}
Problem p00462 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

typedef long long ll;

ll d;
int n, m;

std::vector<int> ds;

ll ans = 0;

int main() {
  while (1) {
    ans = 0;
    std::cin >> d;
    if (d == 0)
      break;
    std::cin >> n >> m;
    ds.clear();
    ds.push_back(0);
    ds.push_back(d);
    ll in;
    for (int i = 0; i < n - 1; ++i) {
      std::cin >> in;
      ds.push_back(in);
    }
    std::sort(ds.begin(), ds.end());
    ll k;
    for (int i = 0; i < m; ++i) {
      std::cin >> k;
      auto it = std::lower_bound(ds.begin(), ds.end(), k);
      int x[2];
      x[0] = (*it);
      --it;
      x[1] = (*it);
      ans += std::min(x[0] - k, k - x[1]);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>

typedef long long ll;

ll d;
int n, m;

std::set<ll> ds;

ll ans = 0;

int main() {
  while (1) {
    ans = 0;
    std::cin >> d;
    if (d == 0)
      break;
    std::cin >> n >> m;
    ds.clear();
    ds.insert(0);
    ds.insert(d);
    ll in;
    for (int i = 0; i < n - 1; ++i) {
      std::cin >> in;
      ds.insert(in);
    }
    ll k;
    for (int i = 0; i < m; ++i) {
      std::cin >> k;
      auto it = std::lower_bound(ds.begin(), ds.end(), k);
      int x[2];
      x[0] = (*it);
      --it;
      x[1] = (*it);
      ans += std::min(x[0] - k, k - x[1]);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
Problem p00463 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
typedef pair<int, int> P;

int main() {
  int n, m, h, k;
  while (cin >> n >> m >> h >> k, n) {
    vector<int> s(n);
    vector<P> stc(m);
    REP(i, n) cin >> s[i];
    REP(i, m) {
      int a, b;
      cin >> a >> b;
      stc[i] = P(b, a - 1);
    }
    sort(stc.begin(), stc.end());
    int memo[100000][2];
    int now[100000];
    int point[1000];
    REP(i, n) now[i] = i;
    REP(i, m) {
      memo[i][0] = now[stc[i].second];
      memo[i][1] = now[stc[i].second + 1];
      swap(now[stc[i].second], now[stc[i].second + 1]);
    }
    int fstans = 0;
    int subans = 0;
    REP(i, n) point[now[i]] = s[i];
    REP(i, k) fstans += point[i];
    REP(i, m) {
      if (memo[i][0] < k && memo[i][1] >= k) {
        subans = min(subans, -point[memo[i][0]] + point[memo[i][1]]);
      }
      if (memo[i][1] < k && memo[i][0] >= k) {
        subans = min(subans, -point[memo[i][1]] + point[memo[i][0]]);
      }
    }
    cout << fstans + subans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
typedef pair<int, int> P;

int main() {
  int n, m, h, k;
  while (cin >> n >> m >> h >> k, n) {
    vector<int> s(n);
    vector<P> stc(m);
    REP(i, n) cin >> s[i];
    REP(i, m) {
      int a, b;
      cin >> a >> b;
      stc[i] = P(b, a - 1);
    }
    sort(stc.begin(), stc.end());
    int memo[1000][2];
    int now[1000];
    int point[1000];
    REP(i, n) now[i] = i;
    REP(i, m) {
      memo[i][0] = now[stc[i].second];
      memo[i][1] = now[stc[i].second + 1];
      swap(now[stc[i].second], now[stc[i].second + 1]);
    }
    int fstans = 0;
    int subans = 0;
    REP(i, n) point[now[i]] = s[i];
    REP(i, k) fstans += point[i];
    REP(i, m) {
      if (memo[i][0] < k) {
        subans = min(subans, -point[memo[i][0]] + point[memo[i][1]]);
      }
      if (memo[i][1] < k) {
        subans = min(subans, -point[memo[i][1]] + point[memo[i][0]]);
      }
    }
    cout << fstans + subans << endl;
  }
  return 0;
}
Problem p00463 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;

void solve() {
  int n, m, h, k;
  while (cin >> n >> m >> h >> k, n || m || h || k) {
    vector<int> point(n);
    for (int i = 0; i < n; ++i) {
      cin >> point[i];
    }
    vector<PP> side_bar;
    for (int i = 0; i < m; ++i) {
      int a, b;
      cin >> a >> b;
      a--;
      side_bar.push_back(PP(P(b, a), P(-1, -1)));
    }
    sort(side_bar.begin(), side_bar.end());
    vector<int> v(n);
    for (int i = 0; i < n; ++i) {
      v[i] = i;
    }
    for (int i = 0; i < m; ++i) {
      int idx = side_bar[i].first.second;
      side_bar[i].second.first = v[idx];
      side_bar[i].second.second = v[idx + 1];
      int t = v[idx];
      v[idx] = v[idx + 1];
      v[idx + 1] = t;
    }
    int sum = 0;
    vector<int> v_rev(n);
    for (int i = 0; i < n; ++i) {
      v_rev[v[i]] = i;
      if (v[i] < k) {
        sum += point[i];
      }
    }
    int ans = sum;
    for (int i = 0; i < m; ++i) {
      int a = side_bar[i].second.first;
      int b = side_bar[i].second.second;
      if (a > b) {
        swap(a, b);
      }
      if (a < k && k <= b) {
        ans = min(ans, sum - point[v_rev[a]] + point[v_rev[b]]);
      }
    }
    cout << ans << endl;
  }
}
int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;

void solve() {
  int n, m, h, k;
  while (cin >> n >> m >> h >> k, n || m || h || k) {
    vector<int> point(n);
    for (int i = 0; i < n; ++i) {
      cin >> point[i];
    }
    vector<PP> side_bar;
    for (int i = 0; i < m; ++i) {
      int a, b;
      cin >> a >> b;
      side_bar.push_back(PP(b, P(a, a + 1)));
    }
    sort(side_bar.begin(), side_bar.end());
    unsigned long long int min_sum = 9999999999999;
    unsigned long long int sum = 0;
    for (int i = 1; i <= k; ++i) {
      int pos = i;
      for (int j = 0; j < m; ++j) {
        if (side_bar[j].second.first == pos) {
          pos = side_bar[j].second.second;
        } else if (side_bar[j].second.second == pos) {
          pos = side_bar[j].second.first;
        }
      }
      sum += point[pos - 1];
      if (sum >= min_sum) {
        break;
      }
    }
    if (min_sum > sum) {
      min_sum = sum;
    }

    for (int l = 0; l < m; ++l) {
      sum = 0;
      for (int i = 1; i <= k; ++i) {
        int pos = i;
        for (int j = 0; j < m; ++j) {
          if (j == l) {
            continue;
          }
          if (side_bar[j].second.first == pos) {
            pos = side_bar[j].second.second;
          } else if (side_bar[j].second.second == pos) {
            pos = side_bar[j].second.first;
          }
        }
        sum += point[pos - 1];
        if (sum >= min_sum) {
          break;
        }
      }
      if (min_sum > sum) {
        min_sum = sum;
      }
    }
    cout << min_sum << endl;
  }
}

int main() {
  solve();
  return (0);
}
Problem p00463 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define P pair<int, int>
using namespace std;

P e[100000];
int main() {
  int a, b, c, d;
  while (cin >> a >> b >> c >> d, a | b || c || d) {
    int f[1000], j[1000], sum = 0;
    for (int g = 0; g < a; g++)
      f[g] = g;
    for (int g = 0; g < a; g++)
      scanf("%d", &j[g]);
    for (int g = 0; g < b; g++) {
      int h, i;
      scanf("%d%d", &h, &i);
      h--;
      e[g] = P(i, h);
    }
    sort(e, e + b);
    for (int g = 0; g < b; g++) {
      swap(f[e[g].second], f[e[g].second + 1]);
    }
    int MAX = 0, k[1000];
    for (int l = 0; l < a; l++) {
      k[l] = l;
      if (f[l] < d)
        sum += j[l];
    }
    for (int g = b - 1; g >= 0; g--) {
      if (f[k[e[g].second]] < d) {
        if (f[k[e[g].second + 1]] >= d) {
          MAX = max(MAX, j[k[e[g].second]] - j[k[e[g].second + 1]]);
        }
      } else {
        if (f[k[e[g].second + 1]] < d) {
          MAX = max(MAX, j[k[e[g].second + 1]] - j[k[e[g].second]]);
        }
      }
      swap(k[e[g].second], k[e[g].second + 1]);
    }
    cout << sum - MAX << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define P pair<int, int>
using namespace std;

P e[10000];
int main() {
  int a, b, c, d;
  while (cin >> a >> b >> c >> d, a | b || c || d) {
    int f[1000], j[1000], sum = 0;
    for (int g = 0; g < a; g++)
      f[g] = g;
    for (int g = 0; g < a; g++)
      scanf("%d", &j[g]);
    for (int g = 0; g < b; g++) {
      int h, i;
      scanf("%d%d", &h, &i);
      h--;
      e[g] = P(i, h);
    }
    sort(e, e + b);
    for (int g = 0; g < b; g++) {
      swap(f[e[g].second], f[e[g].second + 1]);
    }
    int MAX = 0, k[1000];
    for (int l = 0; l < a; l++) {
      k[l] = l;
      if (f[l] < d)
        sum += j[l];
    }
    for (int g = b - 1; g >= 0; g--) {
      if (f[k[e[g].second]] < d) {
        if (f[k[e[g].second + 1]] >= d) {
          MAX = max(MAX, j[k[e[g].second]] - j[k[e[g].second + 1]]);
        }
      } else {
        if (f[k[e[g].second + 1]] < d) {
          MAX = max(MAX, j[k[e[g].second + 1]] - j[k[e[g].second]]);
        }
      }
      swap(k[e[g].second], k[e[g].second + 1]);
    }
    cout << sum - MAX << endl;
  }
}
Problem p00463 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  while (true) {
    int n, m, h, k;
    scanf("%d%d%d%d", &n, &m, &h, &k);
    if (n == 0)
      break;
    static int botpt[1000];
    for (int i = 0; i < n; i++)
      scanf("%d", botpt + i);
    static pair<int, int> bar[100000];
    for (int i = 0; i < m; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      bar[i].first = b;
      bar[i].second = a - 1;
    }
    sort(bar, bar + m);
    static int num[1000];
    static int markL[100000], markR[100000];
    for (int i = 0; i < n; i++)
      num[i] = i;
    for (int i = 0; i < m; i++) {
      int pos = bar[i].second;
      markL[i] = num[pos];
      markR[i] = num[pos + 1];
      swap(num[pos], num[pos + 1]);
    }
    static int toppt[1000];
    for (int i = 0; i < n; i++) {
      toppt[num[i]] = botpt[i];
    }
    int basesum = 0;
    for (int i = 0; i < k; i++)
      basesum += toppt[i];
    int min_dif = 0;
    for (int i = 0; i < m; i++) {
      if (markL[i] < k && markR[i] >= k)
        min_dif = min(min_dif, toppt[markR[i]] - toppt[markL[i]]);
      if (markL[i] >= k && markR[i] < k)
        min_dif = min(min_dif, toppt[markL[i]] - toppt[markR[i]]);
    }
    printf("%d\\n", basesum + min_dif);
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  while (true) {
    int n, m, h, k;
    scanf("%d%d%d%d", &n, &m, &h, &k);
    if (n == 0)
      break;
    static int botpt[1000];
    for (int i = 0; i < n; i++)
      scanf("%d", botpt + i);
    static pair<int, int> bar[100000];
    for (int i = 0; i < m; i++)
      scanf("%d%d", &bar[i].second, &bar[i].first);
    sort(bar, bar + m);
    int num[1000];
    int markL[100000], markR[100000];
    for (int i = 0; i < n; i++)
      num[i] = i;
    for (int i = 0; i < m; i++) {
      int pos = bar[i].second;
      markL[i] = num[pos];
      markR[i] = num[pos + 1];
      swap(num[pos], num[pos + 1]);
    }
    static int toppt[1000];
    for (int i = 0; i < n; i++) {
      toppt[num[i]] = botpt[i];
    }
    int basesum = 0;
    for (int i = 0; i < k; i++)
      basesum += toppt[i];
    int min_dif = 0;
    for (int i = 0; i < m; i++) {
      if (markL[i] < k && markR[i] >= k)
        min_dif = min(min_dif, toppt[markR[i]] - toppt[markL[i]]);
      if (markL[i] >= k && markR[i] < k)
        min_dif = min(min_dif, toppt[markL[i]] - toppt[markR[i]]);
    }
    printf("%d\\n", basesum + min_dif);
  }
  return 0;
}
Problem p00463 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, m, h, k, a[100005], b[100005], c[100005], d[100005], e[100005];
vector<pair<int, int>> x;
int main() {
  while (true) {
    for (int i = 0; i < 100005; i++) {
      a[i] = 0;
      b[i] = 0;
      c[i] = 0;
      d[i] = 0;
    }
    x.clear();
    cin >> n;
    if (n == 0)
      break;
    cin >> m >> h >> k;
    for (int i = 1; i <= n; i++) {
      a[i] = i;
      cin >> b[i];
    }
    for (int i = 0; i < m; i++) {
      int p1, p2;
      cin >> p1 >> p2;
      x.push_back(make_pair(p2, p1));
    }
    sort(x.begin(), x.end());
    for (int i = 0; i < (int)x.size(); i++) {
      c[i] = a[x[i].second];
      d[i] = a[x[i].second + 1];
      swap(a[x[i].second], a[x[i].second + 1]);
    }
    int B = 0;
    for (int i = 1; i <= n; i++) {
      e[a[i]] = b[i];
      if (a[i] <= k)
        B += b[i];
    }
    int maxn = 0;
    for (int i = 0; i < m; i++) {
      if (c[i] <= k && d[i] > k)
        maxn = min(maxn, e[d[i]] - e[c[i]]);
      if (c[i] > k && d[i] <= k)
        maxn = min(maxn, e[c[i]] - e[d[i]]);
    }
    cout << B + maxn << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, h, k, a[1005], b[1005], c[100005], d[100005], e[100005];
vector<pair<int, int>> x;
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    cin >> m >> h >> k;
    for (int i = 1; i <= n; i++) {
      a[i] = i;
      cin >> b[i];
    }
    for (int i = 0; i < m; i++) {
      int p1, p2;
      cin >> p1 >> p2;
      x.push_back(make_pair(p2, p1));
    }
    sort(x.begin(), x.end());
    for (int i = 0; i < (int)x.size(); i++) {
      c[i] = a[x[i].second];
      d[i] = a[x[i].second + 1];
      swap(a[x[i].second], a[x[i].second + 1]);
    }
    int B = 0;
    for (int i = 1; i <= n; i++) {
      e[a[i]] = b[i];
      if (a[i] <= k)
        B += b[i];
    }
    int maxn = 0;
    for (int i = 0; i < m; i++) {
      if (c[i] <= k && d[i] > k)
        maxn = min(maxn, e[d[i]] - e[c[i]]);
      if (c[i] > k && d[i] <= k)
        maxn = min(maxn, e[c[i]] - e[d[i]]);
    }
    cout << B + maxn << endl;
  }
  return 0;
}
Problem p00463 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>

using namespace std;

typedef pair<int, int> mp;

int n, m, k;

int toatai[1005];

int todat[1005];
int tokekka[1005];

mp edg[100005];
int edgcon[100005][2];

int main(void) {

  int a, b;

  while (1) {

    scanf("%d%d%d%d", &n, &m, &a, &k);
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      todat[i] = i;
    }

    for (int i = 1; i <= n; i++) {
      scanf("%d", &(toatai[i]));
    }

    for (int i = 0; i < m; i++) {
      scanf("%d%d", &(edg[i].second), &(edg[i].first));
    }

    sort(edg, edg + m);

    int p, q;
    for (int i = 0; i < m; i++) {
      p = edg[i].second;
      edgcon[i][0] = todat[p];
      edgcon[i][1] = todat[p + 1];
      q = todat[p];
      todat[p] = todat[p + 1];
      todat[p + 1] = q;
    }

    for (int i = 1; i <= n; i++) {
      tokekka[todat[i]] = i;
    }

    /*
    for(int i=1;i<=n;i++){
      printf("kek  %d\\n",tokekka[i]);
    }*/

    int sum = 0;
    for (int i = 1; i <= k; i++) {
      sum += toatai[tokekka[i]];
    }
    int ans = sum;

    // printf("%d\\n",sum);

    for (int i = 0; i < m; i++) {
      int sa;
      if (edgcon[i][0] <= k && edgcon[i][1] > k) {
        sa = toatai[tokekka[edgcon[i][1]]] - toatai[tokekka[edgcon[i][0]]];
      } else if (edgcon[i][1] <= k && edgcon[i][0] > k) {
        sa = toatai[tokekka[edgcon[i][0]]] - toatai[tokekka[edgcon[i][1]]];
      }

      if (ans > sum + sa)
        ans = sum + sa;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <map>
#include <queue>
using namespace std;
int main(void) {
  int n, m, h, k;
  scanf("%d%d%d%d", &n, &m, &h, &k);
  while (n != 0) {
    int ten[1000] = {0};
    int bo[100000] = {0};
    int maps[100000][1000] = {0};
    int i = 0;

    priority_queue<pair<int, int>> que;
    for (i = 0; i < n; i++)
      scanf("%d", &(ten[i]));
    pair<int, int> pa;
    for (i = 0; i < m; i++) {
      scanf("%d%d", &(pa.second), &(pa.first));
      que.push(pa);
    }
    for (i = 0; i < m; i++) {
      pa = que.top();
      bo[i] = pa.second;
      que.pop();
    }
  }
  return 0;
}
Problem p00463 (C++) — buggy submission, verdict: Time Limit Exceeded

// 2011/09/28 Tazoe

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

int main() {
  while (true) {
    int n, m, h, k;
    cin >> n >> m >> h >> k;
    if (n == 0 && m == 0 && h == 0 && k == 0)
      break;

    int tate[1000];
    for (int i = 0; i < n; i++)
      cin >> tate[i];

    vector<int> yoko[1000];
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      yoko[b - 1].push_back(a - 1);
    }

    for (int i = h - 1; i >= 0; i--)
      for (int j = 0; j < yoko[i].size(); j++) {
        int tmp = tate[yoko[i][j]];
        tate[yoko[i][j]] = tate[yoko[i][j] + 1];
        tate[yoko[i][j] + 1] = tmp;
      }

    int sum = 0;
    for (int i = 0; i < k; i++)
      sum += tate[i];

    int min2 = sum;

    int tate2[1000];
    for (int i = 0; i < n; i++)
      tate2[i] = i;

    for (int I = 0; I < h; I++)
      for (int J = 0; J < yoko[I].size(); J++) {

        int M = tate2[yoko[I][J]];
        int N = tate2[yoko[I][J] + 1];

        tate2[yoko[I][J]] = N;
        tate2[yoko[I][J] + 1] = M;

        if (M > N) {
          int tmp = M;
          M = N;
          N = tmp;
        }

        if (M < k && N >= k && tate[M] > tate[N] &&
            min2 > sum - tate[M] + tate[N])
          min2 = sum - tate[M] + tate[N];
      }

    cout << min2 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// 2011/09/12 Tazoe

#include <iostream>
using namespace std;

int main() {
  while (true) {
    int n, m, h, k;
    cin >> n >> m >> h >> k;
    if (n == 0 && m == 0 && h == 0 && k == 0)
      break;

    int tate[1000];
    for (int i = 0; i < n; i++)
      cin >> tate[i];

    int yoko[100000][2];
    for (int i = 0; i < m; i++)
      cin >> yoko[i][0] >> yoko[i][1];

    for (int i = 0; i < m - 1; i++)
      for (int j = i + 1; j < m; j++)
        if (yoko[i][1] > yoko[j][1]) {
          int tmp = yoko[i][0];
          yoko[i][0] = yoko[j][0];
          yoko[j][0] = tmp;
          tmp = yoko[i][1];
          yoko[i][1] = yoko[j][1];
          yoko[j][1] = tmp;
        }

    for (int i = m - 1; i >= 0; i--) {
      int tmp = tate[yoko[i][0] - 1];
      tate[yoko[i][0] - 1] = tate[yoko[i][0]];
      tate[yoko[i][0]] = tmp;
    }

    int sum = 0;
    for (int i = 0; i < k; i++)
      sum += tate[i];

    int min = sum;

    int tate2[1000];
    for (int i = 0; i < n; i++)
      tate2[i] = i;

    for (int I = 0; I < m; I++) {

      int J = tate2[yoko[I][0] - 1];
      int K = tate2[yoko[I][0]];

      tate2[yoko[I][0] - 1] = K;
      tate2[yoko[I][0]] = J;

      if (J > K) {
        int tmp = J;
        J = K;
        K = tmp;
      }

      if (J < k && K >= k && tate[J] > tate[K] && min > sum - tate[J] + tate[K])
        min = sum - tate[J] + tate[K];
    }

    cout << min << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define MAX 1000

using namespace std;

typedef pair<int, int> P;
P score[100001], par[100001];

int main() {
  int n, m, h, k;
  while (scanf("%d%d%d%d", &n, &m, &h, &k)) {
    if (n == 0 && m == 0 && h == 0 && k == 0)
      break;
    vector<P> v;
    int p[MAX + 1], s[MAX + 1];

    for (int i = 0; i < n; i++) {
      s[i] = i;
      scanf("%d", &p[i]);
    }
    for (int i = 0; i < m; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      v.push_back(make_pair(b, a - 1));
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < m; i++) {
      int a, b;
      int j = m - i - 1;
      par[i].first = s[v[i].second];
      par[i].second = s[v[i].second + 1];
      swap(s[v[i].second], s[v[i].second + 1]);
      score[j].first = p[v[j].second];
      score[j].second = p[v[j].second + 1];
      swap(p[v[j].second], p[v[j].second + 1]);
    }
    int sum = accumulate(p, p + k, 0);
    int ans = sum;
    for (int i = 0; i < m; i++) {
      if (par[i].first < k && par[i].second >= k) {
        ans = min(sum - (score[i].second - score[i].first), ans);
      }
    }
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define MAX 100

using namespace std;

typedef pair<int, int> P;
P score[100001], par[100001];

int main() {
  int n, m, h, k;
  while (scanf("%d%d%d%d", &n, &m, &h, &k)) {
    if (n == 0 && m == 0 && h == 0 && k == 0)
      break;
    vector<P> v;
    int p[MAX], s[MAX];

    for (int i = 0; i < n; i++) {
      s[i] = i;
      scanf("%d", &p[i]);
    }
    for (int i = 0; i < m; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      v.push_back(make_pair(b, a - 1));
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < m; i++) {
      int a, b;
      int j = m - i - 1;
      par[i].first = s[v[i].second];
      par[i].second = s[v[i].second + 1];
      swap(s[v[i].second], s[v[i].second + 1]);
      score[j].first = p[v[j].second];
      score[j].second = p[v[j].second + 1];
      swap(p[v[j].second], p[v[j].second + 1]);
    }
    int sum = accumulate(p, p + k, 0);
    int ans = sum;
    for (int i = 0; i < m; i++) {
      if (par[i].first < k && par[i].second >= k) {
        ans = min(sum - (score[i].second - score[i].first), ans);
      }
    }
    printf("%d\\n", ans);
  }
}