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

#include <iostream>
#include <string>

int main() {
  const std::string str_false = "Hoshino";
  const std::string str_true = "Hoshina";

  u_int num_of_data;
  std::cin >> num_of_data;
  std::cin.ignore();

  for (u_int i = 0; i < num_of_data; ++i) {
    std::string text;
    getline(std::cin, text);

    int index = 0;
    while ((index = text.find(str_false, index)) != std::string::npos) {
      text.replace(index, str_false.size(), str_true);
      index += str_false.size();
    }
    std::cout << text << std::endl;
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::replace: __pos (which is 4294967295) > this->size() (which is 7)


Fixed submission:

#include <iostream>
#include <string>

int main() {
  const std::string str_false = "Hoshino";
  const std::string str_true = "Hoshina";

  u_int num_of_data;
  std::cin >> num_of_data;
  std::cin.ignore();

  for (u_int i = 0; i < num_of_data; ++i) {
    std::string text;
    getline(std::cin, text);

    u_int index = 0;
    while ((index = text.find(str_false, index)) != std::string::npos) {
      text.replace(index, str_false.size(), str_true);
      index += str_false.size();
    }
    std::cout << text << std::endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

void Solution() {
  int n;
  int a[11][11];

  while (true) {
    cin >> n;

    if (n == 0) {
      break;
    }

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        a[i][j] = 0;
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> a[i][j];
        a[i][n] += a[i][j];
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= n; j++) {
        a[n][j] += a[i][j];
      }
    }

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        cout << setw(5) << a[i][j];
      }

      cout << endl;
    }
  }
}

int main() {
  Solution();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

void Solution() {
  int n;
  int a[10][10];

  while (true) {
    cin >> n;

    if (n == 0) {
      break;
    }

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        a[i][j] = 0;
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> a[i][j];
        a[i][n] += a[i][j];
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= n; j++) {
        a[n][j] += a[i][j];
      }
    }

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        cout << setw(5) << a[i][j];
      }

      cout << endl;
    }
  }
}

int main() {
  Solution();
  return 0;
}
Problem p00102 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
int n, a[N][N];
signed main(void) {
  ios::sync_with_stdio(false);
  while (cin >> n && n) {
    memset(a, 0, sizeof(a));
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        cin >> a[i][j];
    for (int i = 1, sum; i <= n; i++) {
      sum = 0;
      for (int j = 1; j <= n; j++)
        sum += a[i][j];
      a[i][n + 1] = sum;
    }
    for (int j = 1, sum; j <= n; j++) {
      sum = 0;
      for (int i = 1; i <= n; i++)
        sum += a[i][j];
      a[n + 1][j] = sum;
    }
    int sum = 0;
    for (int i = 1; i <= n; i++)
      sum += a[i][n + 1];
    a[n + 1][n + 1] = sum;
    for (int i = 1; i <= n + 1; i++, cout << endl)
      for (int j = 1; j <= n + 1; j++)
        cout << setw(5) << a[i][j];
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
int n, a[N][N];
signed main(void) {
  ios::sync_with_stdio(false);
  while (cin >> n && n) {
    memset(a, 0, sizeof(a));
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        cin >> a[i][j];
    for (int i = 1, sum; i <= n; i++) {
      sum = 0;
      for (int j = 1; j <= n; j++)
        sum += a[i][j];
      a[i][n + 1] = sum;
    }
    for (int j = 1, sum; j <= n; j++) {
      sum = 0;
      for (int i = 1; i <= n; i++)
        sum += a[i][j];
      a[n + 1][j] = sum;
    }
    int sum = 0;
    for (int i = 1; i <= n; i++)
      sum += a[i][n + 1];
    a[n + 1][n + 1] = sum;
    for (int i = 1; i <= n + 1; i++, cout << endl)
      for (int j = 1; j <= n + 1; j++)
        cout << setw(5) << a[i][j];
  }
}
Problem p00102 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>

using namespace std;

int main() {
  int n[102], tn = 0, ni = 0, all = 0, i = 0, h = 0;
  int data[1024][10];
  int tdata[10] = {0};

  // input
  while (1) {
    cin >> n[ni];
    if (n[ni] == 0)
      break;
    tn += n[ni];

    for (; i < tn; i++) {
      for (int j = 0; j < n[ni]; j++) {
        cin >> data[i][j];
      }
    }
    ni++;
  }

  // output
  for (int k = 0; k < ni; k++) {
    for (int i = 0; i < n[k]; i++, all = 0, h++) {
      for (int j = 0; j < n[k]; j++) {
        printf("%5d", data[h][j]);
        all += data[h][j];
        tdata[j] += data[h][j];
      }
      printf("%5d\\n", all);
    }

    all = 0;
    for (int i = 0; i < n[k]; i++) {
      printf("%5d", tdata[i]);
      all += tdata[i];
    }
    printf("%5d\\n", all);

    for (int i = 0; i < n[k]; i++) {
      tdata[i] = 0;
    }
    all = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>

using namespace std;

int main() {
  int n[127], tn = 0, ni = 0, all = 0, i = 0;
  int data[512][256];
  int tdata[256] = {0};

  // input
  while (1) {
    cin >> n[ni];
    if (n == 0)
      break;
    tn += n[ni];
    ni++;

    for (; i < tn; i++) {
      for (int j = 0; j < n[ni]; j++) {
        cin >> data[i][j];
      }
    }
  }

  // output
  for (int k = 0; k < ni; k++) {
    for (int i = 0; i < tn; i++, all = 0) {
      for (int j = 0; j < n[k]; j++) {
        printf(" %3d", data[i][j]);
        all += data[i][j];
        tdata[j] += data[i][j];
      }
      printf(" %3d\\n", all);
    }

    all = 0;
    for (int i = 0; i < n[k]; i++) {
      printf(" %3d", tdata[i]);
      all += tdata[i];
    }
    printf(" %3d\\n", all);
  }
  return 0;
}
Problem p00102 (C++) — buggy submission, verdict: Runtime Error

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

int hoge(int w);
int getKetasu(int n);

int main() {
  int w;

  while (1) {
    cin >> w;
    if (w == 0) {
      break;
    }
    hoge(w);
  }

  return 0;
}

int hoge(int w) { //?????£???????????????
  int t[100][100];
  int x, y, i;

  for (y = 0; y < w; y++) {
    for (x = 0; x < w; x++) {
      cin >> t[x][y];
    }
  }

  for (y = 0; y < w; y++) {
    t[w][y] = 0;
    for (x = 0; x < w; x++) {
      t[w][y] += t[x][y];
    }
  }
  for (x = 0; x <= w; x++) {
    t[x][w] = 0;
    for (y = 0; y < w; y++) {
      t[x][w] += t[x][y];
    }
  }

  for (y = 0; y <= w; y++) {
    for (x = 0; x <= w; x++) {
      for (i = getKetasu(t[x][y]); i < 5; i++) {
        cout << " ";
      }
      cout << t[x][y];
    }
    cout << endl;
  }

  return 0;
}

int getKetasu(int n) { //?????°?????????
  if (n < 10) {
    return 1;
  } else if (n < 100) {
    return 2;
  } else if (n < 1000) {
    return 3;
  } else if (n < 10000) {
    return 4;
  } else {
    return 5;
  }
}

Error: 0

Fixed submission:

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

int hoge(int w);
int getKetasu(int n);

int main() {
  int w;

  while (1) {
    cin >> w;
    if (w == 0) {
      break;
    }
    hoge(w);
  }

  return 0;
}

int hoge(int w) { //?????£???????????????
  int t[10][10];
  int x, y, i;

  for (y = 0; y < w; y++) {
    for (x = 0; x < w; x++) {
      cin >> t[x][y];
    }
  }

  for (y = 0; y < w; y++) {
    t[w][y] = 0;
    for (x = 0; x < w; x++) {
      t[w][y] += t[x][y];
    }
  }
  for (x = 0; x <= w; x++) {
    t[x][w] = 0;
    for (y = 0; y < w; y++) {
      t[x][w] += t[x][y];
    }
  }

  for (y = 0; y <= w; y++) {
    for (x = 0; x <= w; x++) {
      for (i = getKetasu(t[x][y]); i < 5; i++) {
        cout << " ";
      }
      cout << t[x][y];
    }
    cout << endl;
  }

  return 0;
}

int getKetasu(int n) { //?????°?????????
  if (n < 10) {
    return 1;
  } else if (n < 100) {
    return 2;
  } else if (n < 1000) {
    return 3;
  } else {
    return 4;
  }
}
Problem p00102 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  while (1) {
    int n;
    char line[100];
    cin >> n;
    if (n == 0)
      return 0;
    int list[11][11];
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n + 1; j++) {
        list[i][j] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> list[i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        list[i][n] += list[i][j];
        list[n][i] += list[j][i];
      }
    }
    for (int i = 0; i < n; i++) {
      list[n][n] += list[n][i];
    }
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n + 1; j++) {
        printf("%5d", list[i][j]);
      }
      printf("\\n");
    }
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  while (1) {
    int n;
    char line[100];
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &n);
    if (n == 0)
      return 0;
    int list[11][11];
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n + 1; j++) {
        list[i][j] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      //  char buf[100];
      // fgets(buf,sizeof(buf),stdin);
      for (int j = 0; j < n; j++) {
        // sscanf(buf,"%d",&list[i][j]);
        cin >> list[i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        list[i][n] += list[i][j];
        list[n][i] += list[j][i];
      }
    }
    for (int i = 0; i < n; i++) {
      list[n][n] += list[n][i];
    }
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n + 1; j++) {
        printf("%5d", list[i][j]);
      }
      printf("\\n");
    }
  }
}
Problem p00102 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main(void) {
  int a[11][11];
  int n, i, j;

  scanf("%d", &n);
  while (n != 0) {
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
        scanf("%d", &a[i][j]);

    a[n][n] = 0;
    for (i = 0; i < n; i++) {
      a[i][n] = 0;
      for (j = 0; j < n; j++)
        a[i][n] += a[i][j];
      a[n][n] += a[i][n];
    }

    for (j = 0; j < n; j++) {
      a[n][j] = 0;
      for (i = 0; i < n; i++)
        a[n][j] += a[i][j];
    }

    for (i = 0; i <= n; i++) {
      for (j = 0; j <= n; j++)
        printf("%5d", a[i][j]);
      printf("\\n");
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int a[1000][1000], i, j, n;

int main() {
  scanf("%d", &n);
  while (n != 0) {
    a[n][n] = 0;
    for (i = 0; i < n; i++) {
      a[i][n] = 0;
      a[n][i] = 0;
      for (j = 0; j < n; j++) {
        scanf("%d", &a[i][j]);
        a[i][n] += a[i][j];
      }
      a[n][n] += a[i][n];
    }
    for (j = 0; j < n; j++) {
      for (i = 0; i < n; i++) {
        a[n][j] += a[i][j];
      }
    }
    for (i = 0; i < n + 1; i++) {
      for (j = 0; j < n + 1; j++) {
        printf("% 5d", a[i][j]);
      }
      printf("\\n");
    }
  }

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

#include <stdio.h>

int main(void) {
  int matrix[11][11], i, j, dataset;
  while (scanf("%d", &dataset)) {

    if (dataset == 0)
      break;

    // init add total norm
    for (i = 0; i < dataset + 1; i++) {
      matrix[dataset][i] = 0;
      matrix[i][dataset] = 0;
    }

    // input and calc
    for (i = 0; i < dataset; i++) {
      for (j = 0; j < dataset; j++) {
        scanf("%d", &matrix[i][j]);
        matrix[i][dataset] += matrix[i][j];
      }
    }

    // calc
    for (j = 0; j < dataset + 1; j++) {
      for (i = 0; i < dataset; i++) {
        matrix[dataset][j] += matrix[i][j];
      }
    }

    // output
    for (i = 0; i < dataset + 1; i++) {
      for (j = 0; j < dataset + 1; j++) {
        printf("%5d", matrix[i][j]);
      }
      printf("\\n");
    }
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {
  int matrix[10][10], i, j, dataset;
  while (scanf("%d", &dataset)) {

    if (dataset == 0)
      break;

    // init add total norm
    for (i = 0; i < dataset + 1; i++) {
      matrix[dataset][i] = 0;
      matrix[i][dataset] = 0;
    }

    // input and calc
    for (i = 0; i < dataset; i++) {
      for (j = 0; j < dataset; j++) {
        scanf("%d", &matrix[i][j]);
        matrix[i][dataset] += matrix[i][j];
      }
    }

    // calc
    for (j = 0; j < dataset + 1; j++) {
      for (i = 0; i < dataset; i++) {
        matrix[dataset][j] += matrix[i][j];
      }
    }

    // output
    for (i = 0; i < dataset + 1; i++) {
      for (j = 0; j < dataset + 1; j++) {
        printf("%5d", matrix[i][j]);
      }
      printf("\\n");
    }
  }
}
Problem p00102 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
int main(void) {
  int i, j, hyo[11][11], n, wa[11], wa2[11], sum;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++)
        scanf("%d", &hyo[i][j]);
    }
    for (i = 0; i < n; i++) {
      sum = 0;
      for (j = 0; j < n; j++)
        sum += hyo[i][j];
      wa[i] = sum;
    }
    for (i = 0; i < n; i++) {
      sum = 0;
      for (j = 0; j < n; j++)
        sum += hyo[j][i];
      wa2[i] = sum;
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        printf("%5d", hyo[i][j]);
      }
      printf("%5d", wa[i]);
      printf("\\n");
    }
    sum = 0;
    for (i = 0; i < n; i++) {
      printf("%5d", wa2[i]);
      sum += wa2[i];
    }
    printf("%5d\\n", sum);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
int main(void) {
  int i, j, hyo[11][11], n, wa[4], wa2[4], sum;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++)
        scanf("%d", &hyo[i][j]);
    }
    for (i = 0; i < n; i++) {
      sum = 0;
      for (j = 0; j < n; j++)
        sum += hyo[i][j];
      wa[i] = sum;
    }
    for (i = 0; i < n; i++) {
      sum = 0;
      for (j = 0; j < n; j++)
        sum += hyo[j][i];
      wa2[i] = sum;
    }
    for (i = 0; i < n; i++) {
      printf(" ");
      for (j = 0; j < n; j++) {
        if (hyo[i][j] == 100)
          printf(" %d ", hyo[i][j]);
        if (hyo[i][j] < 100 && hyo[i][j] >= 10)
          printf("  %d ", hyo[i][j]);
      }
      printf("%d\\n", wa[i]);
    }
    printf(" ");
    for (i = 0; i < 4; i++) {
      if (wa2[i] >= 1000)
        printf("%d ", wa2[i]);
      if (wa2[i] >= 100 && wa2[i] < 1000)
        printf(" %d ", wa2[i]);
      else
        printf("   %d ", wa2[i]);
    }
    sum = 0;
    for (i = 0; i < 4; i++) {
      sum += wa[i];
    }
    if (sum == 1000)
      printf("%d\\n", sum);
    if (sum < 1000 && sum >= 10)
      printf("%d\\n", sum);
  }
  return 0;
}
Problem p00102 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
// #include <math.h>
using namespace std;

int log10(int n) {
  int keta = 0;
  do {
    n /= 10;
    keta++;
  } while (n > 0);
  return keta;
}

int main() {
  const int MAX_N = 20;
  int N;
  int table[MAX_N][MAX_N];

  while (1) {
    for (int i = 0; i < MAX_N; i++) {
      for (int j = 0; j < MAX_N; j++) {
        table[i][j] = 0;
      }
    }
    scanf("%d", &N);
    if (!N)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        scanf("%d", &table[i][j]);
      }
    }

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        table[i][N] += table[i][j];
        table[N][j] += table[i][j];
        table[N][N] += table[i][j];
      }
    }

    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= N; j++) {
        for (int k = 0; k < 5 - log10(table[i][j]); k++) {
          printf(" ");
        }
        printf("%d", table[i][j]);
      }
      printf("\\n");
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
using namespace std;

int main() {
  const int MAX_N = 20;
  int N;
  int table[MAX_N][MAX_N];

  while (1) {
    for (int i = 0; i < MAX_N; i++) {
      for (int j = 0; j < MAX_N; j++) {
        table[i][j] = 0;
      }
    }
    scanf("%d", &N);
    if (!N)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        scanf("%d", &table[i][j]);
      }
    }

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        table[i][N] += table[i][j];
        table[N][j] += table[i][j];
        table[N][N] += table[i][j];
      }
    }

    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= N; j++) {
        for (int k = 0; k < 5 - (log10(table[i][j]) + 1); k++) {
          printf(" ");
        }
        printf("%d", &table[i][j]);
      }
      printf("\\n");
    }
  }

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

#include <cstring>
#include <iostream>
using namespace std;
int x[4000][4000], n;
int main() {
  while (true) {
    memset(x, 0, sizeof(x));
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> x[i][j];
        x[i][n] += x[i][j];
        x[n][j] += x[i][j];
        x[n][n] += x[i][j];
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        if (x[i][j] < 10) {
          cout << " ";
        }
        if (x[i][j] < 100) {
          cout << " ";
        }
        if (x[i][j] < 1000) {
          cout << " ";
        }
        if (x[i][j] < 10000) {
          cout << " ";
        }
        cout << x[i][j];
      }
      cout << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int x[10000][10000], n;
int main() {
  while (true) {
    memset(x, 0, sizeof(x));
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> x[i][j];
        x[i][n] += x[i][j];
        x[n][j] += x[i][j];
        x[n][n] += x[i][j];
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        cout << x[i][j] << endl;
      }
    }
  }
}
Problem p00102 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iomanip>
#include <iostream>

int main() {
  int n;
  while (std::cin >> n, n) {
    int data[11][11];
    memset(data, 0, sizeof(data));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        std::cin >> data[i][j];
        data[i][n] += data[i][j];
        data[n][j] += data[i][j];
        data[n][n] += data[i][j];
      }
    }
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n + 1; j++) {
        std::cout << std::setw(5) << std::right << data[i][j];
      }
      std::cout << std::endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iomanip>
#include <iostream>

int main() {
  int n;
  while (std::cin >> n, n) {
    int data[10][10];
    memset(data, 0, sizeof(data));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        std::cin >> data[i][j];
        data[i][n] += data[i][j];
        data[n][j] += data[i][j];
        data[n][n] += data[i][j];
      }
    }
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n + 1; j++) {
        std::cout << std::setw(5) << std::right << data[i][j];
      }
      std::cout << std::endl;
    }
  }
  return 0;
}
Problem p00102 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int main() {
  int n;
  while (cin >> n && n) {
    vector<vint> v(n + 1, vint(n + 1, 0));

    rep(y, n) rep(x, n) cin >> v[y][x];

    int sum = 0;
    rep(y, n) {
      rep(x, n) {
        v[y][n] += v[y][x];
        v[n][x] += v[y][x];
      }

      sum += v[y][n];
    }

    v[n][n] = sum;

    rep(y, n + 1) rep(x, n + 1) printf("%5d%s", v[y][x], (n == x ? "\\n" : ""));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int main() {
  int n;
  while (cin >> n && n) {
    vector<vint> v(n + 1, vint(n + 1));

    rep(i, n) rep(j, n) cin >> v[i][j];

    int sum = 0;
    rep(i, n) {
      rep(j, n) {
        v[i][n] += v[i][j];
        v[n][i] += v[j][i];
      }
      sum += v[i][n];
    }
    v[n][n] = sum;

    rep(i, n + 1) {
      rep(j, n + 1) {
        rep(k, 4 - log10((float)v[i][j])) cout << " ";
        cout << v[i][j];
      }
      cout << endl;
    }
  }
}