Problem p02761 (C++)

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

int main() {
  int N;
  cin >> N;
  int j;
  int M;
  cin >> M;

  vector<int> ans(N);
  long answer;
  answer = 0;
  int i;
  long jousuu;
  vector<int> s(M);
  vector<int> c(M);
  for (i = 0; i < M; i++) {
    cin >> s.at(i);
    // cout << s.at(i);
    cin >> c.at(i);
    // cout << c.at(i);
  }
  ans.at(0) = 1;
  int temp;
  for (i = 0; i < M; i++) {
    for (j = i + 1; j < M; j++) {
      if (c.at(i) < c.at(j)) {
        temp = c.at(i);
        c.at(i) = c.at(j);
        c.at(j) = temp;
        temp = s.at(i);
        s.at(i) = s.at(j);
        s.at(j) = temp;
      }
    }
  }

  for (i = 0; i < M; i++) {
    ans.at(s.at(i) - 1) = c.at(i);
    // cout << ans.at(s.at(i)-1) << c.at(i) << endl;
  }
  jousuu = 1;
  for (i = N - 1; i > 0; i--) {
    jousuu = jousuu * 10;
  }
  for (i = 0; i < N; i++) {
    answer = answer + ans.at(i) * jousuu;
    jousuu = jousuu / 10;
  }

  // かぶりがあったらマイナス１
  for (i = 0; i < M; i++) {
    for (j = 0; j < M; j++) {
      if (i == j) {
        continue;
      }
      if (s.at(i) == s.at(j)) {
        if (c.at(i) != c.at(j)) {
          answer = (-1);
        }
      }
    }
  }

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

  if (N == 1 && c.at(0) == 0) {
    answer = 0;
  }

  /*
  for(i=0;i<M-1;i++){
    if(s.at(i)==s.at(i+1)){
      if(c.at(i)!=c.at(i+1)){
   cout <<  answer<< endl;
        cout << s.at(i) << endl;
    cout << s.at(i+1) << endl;
    cout << c.at(i) << endl;
    cout << c.at(i+1) << endl;
     answer = (-1);
    }
    }
  }
  */
  cout << answer << endl;
}

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

int main() {
  int N;
  cin >> N;
  int j;
  int M;
  cin >> M;

  vector<int> ans(N);
  long answer;
  answer = 0;
  int i;
  long jousuu;
  vector<int> s(M);
  vector<int> c(M);
  for (i = 0; i < M; i++) {
    cin >> s.at(i);
    // cout << s.at(i);
    cin >> c.at(i);
    // cout << c.at(i);
  }
  ans.at(0) = 1;
  int temp;
  for (i = 0; i < M; i++) {
    for (j = i + 1; j < M; j++) {
      if (c.at(i) < c.at(j)) {
        temp = c.at(i);
        c.at(i) = c.at(j);
        c.at(j) = temp;
        temp = s.at(i);
        s.at(i) = s.at(j);
        s.at(j) = temp;
      }
    }
  }

  for (i = 0; i < M; i++) {
    ans.at(s.at(i) - 1) = c.at(i);
    // cout << ans.at(s.at(i)-1) << c.at(i) << endl;
  }
  jousuu = 1;
  for (i = N - 1; i > 0; i--) {
    jousuu = jousuu * 10;
  }
  for (i = 0; i < N; i++) {
    answer = answer + ans.at(i) * jousuu;
    jousuu = jousuu / 10;
  }

  // かぶりがあったらマイナス１
  for (i = 0; i < M; i++) {
    for (j = 0; j < M; j++) {
      if (i == j) {
        continue;
      }
      if (s.at(i) == s.at(j)) {
        if (c.at(i) != c.at(j)) {
          answer = (-1);
        }
      }
    }
  }

  for (i = 0; i < M; i++) {
    if (s.at(i) == 1 && c.at(i) == 0) {
      answer = (-1);
    }
  }
  if (M != 0) {
    if (N == 1 && s.at(0) == 1 && c.at(0) == 0) {
      answer = 0;
    }
  } else {
    if (N == 1) {
      answer = 0;
    }
  }

  /*
  for(i=0;i<M-1;i++){
    if(s.at(i)==s.at(i+1)){
      if(c.at(i)!=c.at(i+1)){
   cout <<  answer<< endl;
        cout << s.at(i) << endl;
    cout << s.at(i+1) << endl;
    cout << c.at(i) << endl;
    cout << c.at(i+1) << endl;
     answer = (-1);
    }
    }
  }
  */
  cout << answer << endl;
}