Problem p02761 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int lint;
typedef pair<int, int> IP;
typedef pair<lint, lint> LLP;
typedef pair<char, char> CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v, hoge) upper_bound(v.begin(), v.end(), hoge)
#define lower(v, hoge) lower_bound(v.begin(), v.end(), hoge)
#define llower(v, hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v, hoge) *upper_bound(v.begin(), v.end(), hoge)
#define toupper(S) transform(S.begin(), S.end(), S.begin(), toupper)
#define tolower(S) transform(S.begin(), S.end(), S.begin(), tolower)
// const lint MOD = pow(10, 9) + 7;

int main() {
  int N, M;
  cin >> N >> M;
  vector<IP> SC(M);
  rep(i, M) { cin >> SC[i].first >> SC[i].second; }
  sort(SC);
  /*rep(i, M) {
          cout << SC[i].first << ' ' << SC[i].second << endl;
  }*/
  vector<int> ans(N, 0);

  if (M >= 2) {
    rep(i, M - 1) {
      if (SC[i].first == SC[i + 1].first && SC[i].second != SC[i + 1].second) {
        cout << -1 << endl;
        return 0;
      }
    }
  }

  if (N != 1) {
    if (SC[0].first == 1 && SC[0].second == 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  rep(i, M) { ans[SC[i].first - 1] = SC[i].second; }

  if (N == 1) {
    cout << ans[0] << endl;
  }

  else {
    rep(i, N) {
      if (i == 0) {
        if (ans[i] == 0) {
          cout << 1;
        } else {
          cout << ans[i];
        }
      } else {
        cout << ans[i];
      }
    }
    cout << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int lint;
typedef pair<int, int> IP;
typedef pair<lint, lint> LLP;
typedef pair<char, char> CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v, hoge) upper_bound(v.begin(), v.end(), hoge)
#define lower(v, hoge) lower_bound(v.begin(), v.end(), hoge)
#define llower(v, hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v, hoge) *upper_bound(v.begin(), v.end(), hoge)
#define toupper(S) transform(S.begin(), S.end(), S.begin(), toupper)
#define tolower(S) transform(S.begin(), S.end(), S.begin(), tolower)
// const lint MOD = pow(10, 9) + 7;

int main() {
  int N, M;
  cin >> N >> M;
  if (M == 0) {
    if (N == 1) {
      cout << 0 << endl;
      return 0;
    } else if (N == 2) {
      cout << 10 << endl;
      return 0;
    } else {
      cout << 100 << endl;
      return 0;
    }
  }
  vector<IP> SC(M);
  rep(i, M) { cin >> SC[i].first >> SC[i].second; }
  sort(SC);
  /*rep(i, M) {
          cout << SC[i].first << ' ' << SC[i].second << endl;
  }*/
  vector<int> ans(N, 0);

  if (M >= 2) {
    rep(i, M - 1) {
      if (SC[i].first == SC[i + 1].first && SC[i].second != SC[i + 1].second) {
        cout << -1 << endl;
        return 0;
      }
    }
  }

  if (N != 1) {
    if (SC[0].first == 1 && SC[0].second == 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  rep(i, M) { ans[SC[i].first - 1] = SC[i].second; }

  if (N == 1) {
    cout << ans[0] << endl;
  }

  else {
    rep(i, N) {
      if (i == 0) {
        if (ans[i] == 0) {
          cout << 1;
        } else {
          cout << ans[i];
        }
      } else {
        cout << ans[i];
      }
    }
    cout << endl;
  }
}