Problem p03137 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2147483647
#define int long long
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
// typedef long long int ll;
signed main() {

  int n, m, x[111111], ans = 0;
  ;
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> x[i];
  sort(x, x + m);
  pair<int, int> v[111111];
  for (int i = 0; i < m - 1; i++) {
    v[i] = make_pair(x[i + 1] - x[i], x[i + 1]);
  }
  sort(v, v + m - 1);

  /*for(int i=0;i<m-1;i++){
      cout<<v[i].first<<" "<<v[i].second<<endl;
  }*/
  // cout<<endl;

  int xf[222222] = {0};
  for (int i = m - 2; m - 2 - i != n - 1; i--) {
    xf[v[i].second] = 1;
    // cout<<v[i].second<<endl;
  }
  xf[x[0]] = 1;
  int place = x[0], co = 0;
  for (int i = 0; i < m; i++) {
    if (xf[x[i]] == 1 && i != 0) {
      ans += x[i - 1] - place;
      // cout<<place<<" "<<x[i]<<endl;
      place = x[i];
      // co++;
    }
    // if(co==n)break;
  }
  ans += x[m - 1] - place;
  cout << ans << endl;
}

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2147483647
#define int long long
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
// typedef long long int ll;
signed main() {

  int n, m, x[111111], ans = 0;
  ;
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> x[i];
  sort(x, x + m);
  pair<int, int> v[111111];
  for (int i = 0; i < m - 1; i++) {
    v[i] = make_pair(x[i + 1] - x[i], x[i + 1]);
  }
  sort(v, v + m - 1);

  /*for(int i=0;i<m-1;i++){
      cout<<v[i].first<<" "<<v[i].second<<endl;
  }*/
  // cout<<endl;

  unordered_map<int, int> xf;
  for (int i = m - 2; m - 2 - i != n - 1; i--) {
    xf[v[i].second] = 1;
    // cout<<v[i].second<<endl;
  }
  xf[x[0]] = 1;
  int place = x[0], co = 0;
  for (int i = 0; i < m; i++) {
    if (xf[x[i]] == 1 && i != 0) {
      ans += x[i - 1] - place;
      // cout<<place<<" "<<x[i]<<endl;
      place = x[i];
      // co++;
    }
    // if(co==n)break;
  }
  ans += x[m - 1] - place;
  cout << ans << endl;
}