Problem p02761 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int fuka = 0;
  int N, M;
  cin >> N >> M;
  vector<int> s(M);
  vector<int> c(M);
  for (int i = 0; i < M; i++) {
    cin >> s.at(i) >> c.at(i);
  }

  // 1keta no toki

  if (N == 1) {

    sort(c.begin(), c.end());
    if (c.at(0) != c.at(M - 1)) {
      fuka = 1;
    }

    if (fuka == 1) {
      cout << -1 << endl;
    } else {
      cout << c.at(0) << endl;
    }

  } else {

    // 2,3 keta no toki

    for (int i = 0; i < M; i++) {
      if (s.at(i) == 1 && c.at(i) == 0) {
        fuka = 1;
      }
    }

    for (int i = 0; i < M; i++) {
      for (int j = 0; j < M; j++) {
        if (s.at(i) == s.at(j) && c.at(i) != c.at(j)) {
          fuka = 1;
        }
      }
    }

    // kokomade reigai shori

    int r1, r2, r3;
    int flag1 = 0;
    int flag2 = 0;
    int flag3 = 0;

    if (N == 3) {

      for (int i = 0; i < M; i++) {
        if (s.at(i) == 1) {
          r1 = c.at(i);
          flag1 = 1;
        }
      }
      if (flag1 == 0) {
        r1 = 1;
      }

      for (int i = 0; i < M; i++) {
        if (s.at(i) == 2) {
          r2 = c.at(i);
          flag2 = 1;
        }
      }
      if (flag2 == 0) {
        r2 = 0;
      }

      for (int i = 0; i < M; i++) {
        if (s.at(i) == 3) {
          r3 = c.at(i);
          flag3 = 1;
        }
      }
      if (flag3 == 0) {
        r3 = 0;
      }

      if (fuka == 0) {
        cout << r1 * 100 + r2 * 10 + r3 << endl;
      } else {
        cout << -1 << endl;
      }

      // N=3 no kakko
    }

    if (N == 2) {

      for (int i = 0; i < M; i++) {
        if (s.at(i) == 1) {
          r1 = c.at(i);
          flag1 = 1;
        }
      }
      if (flag1 == 0) {
        r1 = 1;
      }

      for (int i = 0; i < M; i++) {
        if (s.at(i) == 2) {
          r2 = c.at(i);
          flag2 = 1;
        }
      }
      if (flag2 == 0) {
        r2 = 0;
      }

      if (fuka == 0) {
        cout << r1 * 10 + r2 << endl;
      } else {
        cout << -1 << endl;
      }
    }

    // N=2,3 no kakko
  }
}

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

int main() {
  int fuka = 0;
  int N, M;
  cin >> N >> M;
  vector<int> s(M);
  vector<int> c(M);
  for (int i = 0; i < M; i++) {
    cin >> s.at(i) >> c.at(i);
  }

  // 1keta no toki
  if (N == 1 && M == 0) {
    cout << 0 << endl;
  }
  if (N == 1 && M > 0) {

    sort(c.begin(), c.end());
    if (c.at(0) != c.at(M - 1)) {
      fuka = 1;
    }

    if (fuka == 1) {
      cout << -1 << endl;
    } else {
      cout << c.at(0) << endl;
    }

  } else {

    // 2,3 keta no toki

    for (int i = 0; i < M; i++) {
      if (s.at(i) == 1 && c.at(i) == 0) {
        fuka = 1;
      }
    }

    for (int i = 0; i < M; i++) {
      for (int j = 0; j < M; j++) {
        if (s.at(i) == s.at(j) && c.at(i) != c.at(j)) {
          fuka = 1;
        }
      }
    }

    // kokomade reigai shori

    int r1, r2, r3;
    int flag1 = 0;
    int flag2 = 0;
    int flag3 = 0;

    if (N == 3) {

      for (int i = 0; i < M; i++) {
        if (s.at(i) == 1) {
          r1 = c.at(i);
          flag1 = 1;
        }
      }
      if (flag1 == 0) {
        r1 = 1;
      }

      for (int i = 0; i < M; i++) {
        if (s.at(i) == 2) {
          r2 = c.at(i);
          flag2 = 1;
        }
      }
      if (flag2 == 0) {
        r2 = 0;
      }

      for (int i = 0; i < M; i++) {
        if (s.at(i) == 3) {
          r3 = c.at(i);
          flag3 = 1;
        }
      }
      if (flag3 == 0) {
        r3 = 0;
      }

      if (fuka == 0) {
        cout << r1 * 100 + r2 * 10 + r3 << endl;
      } else {
        cout << -1 << endl;
      }

      // N=3 no kakko
    }

    if (N == 2) {

      for (int i = 0; i < M; i++) {
        if (s.at(i) == 1) {
          r1 = c.at(i);
          flag1 = 1;
        }
      }
      if (flag1 == 0) {
        r1 = 1;
      }

      for (int i = 0; i < M; i++) {
        if (s.at(i) == 2) {
          r2 = c.at(i);
          flag2 = 1;
        }
      }
      if (flag2 == 0) {
        r2 = 0;
      }

      if (fuka == 0) {
        cout << r1 * 10 + r2 << endl;
      } else {
        cout << -1 << endl;
      }
    }

    // N=2,3 no kakko
  }
}