Problem p02706 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <sstream>
#define ll long long int
#include <stdlib.h>
#include <vector>
#define pi acos(-1)
#define maxm 100005
#define pb push_back
#define MOD 1e9 + 7
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
/// s1.substr(1,3);///STARTING FROM INDEX 1, 3 LNEGTH SUBSTRING.
/// s1.find("adn")///RETURNS STARTING INDEX OF THE SUBSTRING
///"adn" IF IT EXISTS IN THE STRING S1.///
/// s1.find("adn",int pos);///RETURNS STARTING INDEX OF THE
/// SUBSTRING "adn" SEARCHING FROM INDEX POS OF STRING s1.///
/// string::npos returns a garbage value if not found...
/// reverse(s1.begin(),s1.end())-> to reverse a string.
/// next_permutation(f,f+n)->f array er next permutation return korbe..
/// str.erase(1,2)->deletes 2 char from index 1..
/// for(auto i:p2)->i=p2.begin() theke p2.end() er ag porjonto..
/// max_element(v,v+4);->return a pointer to the max element
/// in a array/vector.. v from 1st element to 4th element.
/// same for min_element(v,v+4);
///__builtin_popcount(a);->counts how many number of one's
///  in a in binary representation.
/// accumulate(l1+1,l1+10+1,0LL)=>it perform the sum from l1[1] to
/// l1[10] and 0 is the initial sum.
/// cout<<fixed<<setprecision(12)<<x14<<endl;//=>x14 er
/// doshomik er por 12 ta 0 thakbe.
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define gcd(a, b) __gcd(a, b)
using namespace std;

ll l1[200005], vis[200005], l3[200005], s = 0, m = 0, mn = 0;
ll dx[] = {1, 0, -1, 0};
ll dy[] = {0, -1, 0, 1};

// vector<ll>v[5];
// set<ll>v2;
// map<ll,ll>m2;
// vector<pair<ll,ll>>p2;
// vector<pair<ll,ll>>p3;

char s2[maxm];
// int sum[max];
int tree[maxm * 4];
// int prop[max*3];
void STB(int at, int L, int R) {
  // int sum[at]=0;
  if (L == R) {
    tree[at] = (s2[L - 1] - '0');
    return;
  }
  int mid = (L + R) / 2;
  STB(2 * at, L, mid);
  STB(2 * at + 1, mid + 1, R);
  // tree[at]=tree[2*at]+tree[2*at+1];
  // return sum[at];
}

void update(int at, int L, int R, int pos1, int pos2) {
  if (L > pos2 || R < pos1)
    return;

  if (L >= pos1 && R <= pos2) {
    tree[at] ^= 1;
    return;
  }
  int mid = (L + R) >> 1;
  //    if(pos2<=mid)update((at<<1)+1,L,mid,pos1,pos2);
  //    else if(pos1>mid)update((at<<1)+2,mid+1,R,pos1,pos2);
  //    else{
  //        update((at<<1)+1,L,mid,pos1,mid);
  //        update((at<<1)+2,mid+1,R,mid+1,pos2);
  //    }
  update((at << 1), L, mid, pos1, pos2);
  update((at << 1) + 1, mid + 1, R, pos1, pos2);
  // tree[at]=tree[(at<<1)+1]+tree[(at<<1)+2]+prop[at]*(R-L+1);
}

int query(int at, int L, int R, int l) {
  // if(L<=l && l<=R)return tree[at];
  if (L == R)
    return tree[at];
  if (L > l && l > R)
    return 0;

  int mid = (L + R) >> 1;
  //    if(l<=mid)query((at<<1)+1,L,mid,l,carry+prop[at]);
  //    else query((at<<1)+2,mid+1,R,l,carry+prop[at]);
  if (l <= mid)
    return tree[at] ^ query((at << 1), L, mid, l);
  else
    return tree[at] ^ query((at << 1) + 1, mid + 1, R, l);
  // return ;
}

/// reverse sorting function.
bool revp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  if (a.first != b.first)
    return a.first > b.first;
  else
    return a.second < b.second;
}
bool revp2(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return a.second < b.second;
}

ll ones(ll n) {
  ll s = 0;
  while (n > 0) {
    if (n & 1)
      s++;
    n = n >> 1;
    // cout<<n<<endl;
  }
  return s;
}
vector<ll> vp2[200005];
vector<ll> vp;
map<ll, ll> m3;
map<ll, ll> m4;
// m3[1]=0;
void dfs(ll a) {
  if (!vis[a]) {
    vis[a] = 1;
    // if(a!=1)vp.push_back(m3[a]-(vp2[a].size()-1));
    // l3[a]+=vp2[a].size();
    for (int i = 0; i < vp2[a].size(); i++) {
      if (m3[vp2[a][i]] == 0 && vp2[a][i] != 1)
        m3[vp2[a][i]] = m3[a] + 1;
      // cout<<"node->"<<vp2[a][i]<<", level->"<<m3[vp2[a][i]]<<endl;
      // m4[m3[a]+1]++;
      // m=m3[vp2[a][i]];///koyta level
      if (!vis[vp2[a][i]]) {
        dfs(vp2[a][i]);
        vis[a] += vis[vp2[a][i]];
      }
    }
    //}
  } else
    return;
}

int main() {
  // IOS;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ll t, i = 1, j, k, x, y, q = 0, n, c, m, z, z2, l, r, cases = 0, d, p, nl, np,
        a, e = 1, b, x2 = -1, x1 = 1000000005, x3 = 0, x4 = 0, x5 = 0, x6 = 0,
        x7 = 0, x8 = 0;
  double x10 = 100000, x14, x15, x16, x17, x18;
  char x11, y1 = 'a', y2, y3;
  int x12 = 0;
  string s3, s4, s5, s6, s7, s8;
  queue<ll> q2;
  stack<ll> st;
  set<ll> y4;
  // vector<ll>vp;
  // vector<ll>vp2[200005];
  //    vector<ll>vp3;
  //    vector<ll>vp4;
  map<ll, ll> mp;
  // set<ll>y4;
  map<ll, ll> m2;
  map<ll, ll>::iterator it;
  // map<ll,ll>m3;

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> l1[i];
    x3 += l1[i];
  }
  if (x3 > n)
    cout << -1 << endl;
  else
    cout << n - x3 << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <sstream>
#define ll long long int
#include <stdlib.h>
#include <vector>
#define pi acos(-1)
#define maxm 100005
#define pb push_back
#define MOD 1e9 + 7
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
/// s1.substr(1,3);///STARTING FROM INDEX 1, 3 LNEGTH SUBSTRING.
/// s1.find("adn")///RETURNS STARTING INDEX OF THE SUBSTRING
///"adn" IF IT EXISTS IN THE STRING S1.///
/// s1.find("adn",int pos);///RETURNS STARTING INDEX OF THE
/// SUBSTRING "adn" SEARCHING FROM INDEX POS OF STRING s1.///
/// string::npos returns a garbage value if not found...
/// reverse(s1.begin(),s1.end())-> to reverse a string.
/// next_permutation(f,f+n)->f array er next permutation return korbe..
/// str.erase(1,2)->deletes 2 char from index 1..
/// for(auto i:p2)->i=p2.begin() theke p2.end() er ag porjonto..
/// max_element(v,v+4);->return a pointer to the max element
/// in a array/vector.. v from 1st element to 4th element.
/// same for min_element(v,v+4);
///__builtin_popcount(a);->counts how many number of one's
///  in a in binary representation.
/// accumulate(l1+1,l1+10+1,0LL)=>it perform the sum from l1[1] to
/// l1[10] and 0 is the initial sum.
/// cout<<fixed<<setprecision(12)<<x14<<endl;//=>x14 er
/// doshomik er por 12 ta 0 thakbe.
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define gcd(a, b) __gcd(a, b)
using namespace std;

ll l1[200005], vis[200005], l3[200005], s = 0, m = 0, mn = 0;
ll dx[] = {1, 0, -1, 0};
ll dy[] = {0, -1, 0, 1};

// vector<ll>v[5];
// set<ll>v2;
// map<ll,ll>m2;
// vector<pair<ll,ll>>p2;
// vector<pair<ll,ll>>p3;

char s2[maxm];
// int sum[max];
int tree[maxm * 4];
// int prop[max*3];
void STB(int at, int L, int R) {
  // int sum[at]=0;
  if (L == R) {
    tree[at] = (s2[L - 1] - '0');
    return;
  }
  int mid = (L + R) / 2;
  STB(2 * at, L, mid);
  STB(2 * at + 1, mid + 1, R);
  // tree[at]=tree[2*at]+tree[2*at+1];
  // return sum[at];
}

void update(int at, int L, int R, int pos1, int pos2) {
  if (L > pos2 || R < pos1)
    return;

  if (L >= pos1 && R <= pos2) {
    tree[at] ^= 1;
    return;
  }
  int mid = (L + R) >> 1;
  //    if(pos2<=mid)update((at<<1)+1,L,mid,pos1,pos2);
  //    else if(pos1>mid)update((at<<1)+2,mid+1,R,pos1,pos2);
  //    else{
  //        update((at<<1)+1,L,mid,pos1,mid);
  //        update((at<<1)+2,mid+1,R,mid+1,pos2);
  //    }
  update((at << 1), L, mid, pos1, pos2);
  update((at << 1) + 1, mid + 1, R, pos1, pos2);
  // tree[at]=tree[(at<<1)+1]+tree[(at<<1)+2]+prop[at]*(R-L+1);
}

int query(int at, int L, int R, int l) {
  // if(L<=l && l<=R)return tree[at];
  if (L == R)
    return tree[at];
  if (L > l && l > R)
    return 0;

  int mid = (L + R) >> 1;
  //    if(l<=mid)query((at<<1)+1,L,mid,l,carry+prop[at]);
  //    else query((at<<1)+2,mid+1,R,l,carry+prop[at]);
  if (l <= mid)
    return tree[at] ^ query((at << 1), L, mid, l);
  else
    return tree[at] ^ query((at << 1) + 1, mid + 1, R, l);
  // return ;
}

/// reverse sorting function.
bool revp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  if (a.first != b.first)
    return a.first > b.first;
  else
    return a.second < b.second;
}
bool revp2(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return a.second < b.second;
}

ll ones(ll n) {
  ll s = 0;
  while (n > 0) {
    if (n & 1)
      s++;
    n = n >> 1;
    // cout<<n<<endl;
  }
  return s;
}
vector<ll> vp2[200005];
vector<ll> vp;
map<ll, ll> m3;
map<ll, ll> m4;
// m3[1]=0;
void dfs(ll a) {
  if (!vis[a]) {
    vis[a] = 1;
    // if(a!=1)vp.push_back(m3[a]-(vp2[a].size()-1));
    // l3[a]+=vp2[a].size();
    for (int i = 0; i < vp2[a].size(); i++) {
      if (m3[vp2[a][i]] == 0 && vp2[a][i] != 1)
        m3[vp2[a][i]] = m3[a] + 1;
      // cout<<"node->"<<vp2[a][i]<<", level->"<<m3[vp2[a][i]]<<endl;
      // m4[m3[a]+1]++;
      // m=m3[vp2[a][i]];///koyta level
      if (!vis[vp2[a][i]]) {
        dfs(vp2[a][i]);
        vis[a] += vis[vp2[a][i]];
      }
    }
    //}
  } else
    return;
}

int main() {
  // IOS;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ll t, i = 1, j, k, x, y, q = 0, n, c, m, z, z2, l, r, cases = 0, d, p, nl, np,
        a, e = 1, b, x2 = -1, x1 = 1000000005, x3 = 0, x4 = 0, x5 = 0, x6 = 0,
        x7 = 0, x8 = 0;
  double x10 = 100000, x14, x15, x16, x17, x18;
  char x11, y1 = 'a', y2, y3;
  int x12 = 0;
  string s3, s4, s5, s6, s7, s8;
  queue<ll> q2;
  stack<ll> st;
  set<ll> y4;
  // vector<ll>vp;
  // vector<ll>vp2[200005];
  //    vector<ll>vp3;
  //    vector<ll>vp4;
  map<ll, ll> mp;
  // set<ll>y4;
  map<ll, ll> m2;
  map<ll, ll>::iterator it;
  // map<ll,ll>m3;

  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    cin >> l1[i];
    x3 += l1[i];
  }
  if (x3 > n)
    cout << -1 << endl;
  else
    cout << n - x3 << endl;

  return 0;
}