Problem p00463 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "stdio.h"
#include "stdlib.h"

int s[1000];

typedef struct {
  int a;
  int b;
  int w1;
  int w2;
} yoko;

yoko w[100000];

int z[1000];

int ten[1000];

int sortw(const void *a, const void *b) {
  if (((yoko *)a)[0].a < ((yoko *)b)[0].a) {
    return -1;
  } else if (((yoko *)a)[0].a == ((yoko *)b)[0].a) {
    return 0;
  } else {
    return 1;
  }
}

int ans[1000];

int main() {
  int nww = 0;
  while (1) {
    int n, m, h, k;
    scanf("%d %d %d %d", &n, &m, &h, &k);
    if (n == 0 && m == 0 && h == 0 && k == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      scanf("%d", &(s[i]));
    }
    for (int i = 0; i < m; i++) {
      scanf("%d %d", &(w[i].b), &(w[i].a));
    }
    qsort(w, m, sizeof(yoko), sortw);
    for (int i = 0; i < n; i++) {
      z[i] = i;
    }
    int d;
    for (int i = 0; i < m; i++) {
      w[i].w1 = z[w[i].b];
      w[i].w2 = z[w[i].b - 1];
      d = z[w[i].b];
      z[w[i].b] = z[w[i].b - 1];
      z[w[i].b - 1] = d;
    }
    for (int i = 0; i < n; i++) {
      ten[z[i]] = s[i];
    }
    int min;
    int www = 0;
    for (int i = 0; i < k; i++) {
      www = www + ten[i];
    }
    min = www;
    for (int i = 0; i < m; i++) {
      d = ten[w[i].w1];
      ten[w[i].w1] = ten[w[i].w2];
      ten[w[i].w2] = d;
      www = 0;
      for (int i = 0; i < k; i++) {
        www = www + ten[i];
      }
      if (www < min) {
        min = www;
      }
      d = ten[w[i].w1];
      ten[w[i].w1] = ten[w[i].w2];
      ten[w[i].w2] = d;
    }
    ans[nww] = min;
    nww++;
  }
  for (int i = 0; i < nww; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;

long long int d[100000];
long long int k[10000];

typedef struct {
  long long int w;
  char flg;
} WWW;

WWW w[200000];

long long int a[100];

int main() {
  int n = 0;
  while (1) {
    long long int dw;
    scanf("%lld", &dw);
    if (dw == 0) {
      break;
    }
    int nn, m;
    scanf("%d", &nn);
    scanf("%d", &m);
    d[0] = 0;
    for (int i = 1; i < nn; i++) {
      scanf("%lld", &(d[i]));
    }
    for (int i = 0; i < m; i++) {
      scanf("%lld", &(k[i]));
    }
    sort(d, d + nn);
    sort(k, k + m);
    int nw = 0;
    int nww = 0;
    for (int i = 0; i < nn; i++) {
    aa:
      if (nww < m) {
        if (d[i] > k[nww]) {
          w[nw].w = k[nww];
          w[nw].flg = 1;
          nw++;
          nww++;
          goto aa;
        }
      }
      w[nw].w = d[i];
      w[nw].flg = 0;
      nw++;
    }
    for (int i = nww; i < m; i++) {
      w[nw].w = k[i];
      w[nw].flg = 1;
      nw++;
      nww++;
    }
    long long int all = 0;
    for (int i = 0; i < nw; i++) {
      if (w[i].flg == 1) {
        long long int sho, sho2;
        for (int ii = 0; ii < nw; ii++) {
          if (w[(i + ii) % nw].flg == 0) {
            sho = (dw + w[(i + ii) % nw].w - w[i].w) % dw;
            break;
          }
        }
        for (int ii = 0; ii < nw; ii++) {
          if (w[(nw + i - ii) % nw].flg == 0) {
            sho2 = (dw + w[i].w - w[(nw + i - ii) % nw].w) % dw;
            if (sho2 < sho) {
              sho = sho2;
            }
            break;
          }
        }
        all = all + sho;
      }
    }
    a[n] = all;
    n++;
  }
  for (int i = 0; i < n; i++) {
    printf("%lld\\n", a[i]);
  }
  return 0;
}