Problem p02655 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define re register
struct eg {
  int to, c;
  eg *nx;
} e[200100], *la[100100], *cnt = e;
inline void addE(re int a, re int b, re int c) {
  *++cnt = (eg){b, c, la[a]};
  la[a] = cnt;
  *++cnt = (eg){a, c, la[b]};
  la[b] = cnt;
}
int d[100100], dd[100100], dd1[100100], aa[100100], bb[100100], cc[100100], bl;
long long num, nc, vl1[100100], vl2[100100];
bool vis[200100];
std::vector<long long> vv, vk, v1;
void dfs(re int a) {
  vis[a] = 1;
  if (d[a] > 2)
    bl = 1;
  for (re eg *i = la[a]; i; i = i->nx) {
    if (!vis[i->to]) {
      dfs(i->to);
    }
    nc += i->c - 1;
  }
}
int main() {
  //	freopen("01-02.txt","r",stdin);
  re int n, m, ans = 0, a, b, c;
  scanf("%d%d", &n, &m);
  for (re int i = 1; i <= m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    aa[i] = a;
    bb[i] = b;
    cc[i] = c;
    if (a == b) {
      d[a] += 3;
      dd[a]++;
      dd1[a]++;
    } else {
      d[a]++;
      d[b]++;
      addE(a, b, c);
      dd[a]++;
      dd1[b]++;
      ans++;
    }
  }
  for (re int i = 1; i <= m; i++) {
    if (aa[i] == bb[i] && cc[i] > 1) { // if(d[aa[i]]==3)printf("****%d %d %d
                                       // %d\\n",aa[i],d[aa[i]],cc[i],v1.size());
      if (d[aa[i]] != 3)
        vv.push_back(cc[i] - 1);
      else
        v1.push_back(cc[i] - 2);
    }
  }
  for (re int i = 1; i <= n; i++) {
    if (!dd1[i] && dd[i]) {
      puts("-1");
      return 0;
    }
    if (!vis[i]) {
      bl = 0;
      nc = 0;
      dfs(i);
      nc /= 2; // printf("****%d %d %lld\\n",i,bl,nc);
      if (bl) {
        num += nc; // printf("**a**%d %lld\\n",i,nc);
      } else {
        vk.push_back(nc);
      }
    }
  }
  // printf("****%lld %d %d\\n",num,ans,vk.size());
  std::sort(vv.begin(), vv.end());
  std::sort(vk.begin(), vk.end());
  std::sort(v1.begin(), v1.end());
  if (vk.empty()) {
    printf("%d\\n", ans);
    return 0;
  }
  if (!num) {
    if (!vv.empty())
      ans++, num += *vv.rbegin(), vv.pop_back();
    else {
      puts("-1");
      return 0;
    }
  }
  while (num && !vk.empty()) {
    num += *vk.rbegin() - 1;
    ans++;
    vk.pop_back();
  }
  ans += vk.size();
  //	printf("***%d %lld %d %d
  //%d\\n",vk.size(),*vk.rbegin(),v1.size(),vv.size(),ans);
  std::reverse(v1.begin(), v1.end());
  std::reverse(vv.begin(), vv.end());
  for (re int i = 1; i <= v1.size(); i++)
    vl1[i] = vl1[i - 1] + v1[i - 1];
  for (re int i = 1; i <= vv.size(); i++)
    vl2[i] = vl2[i - 1] + vv[i - 1];
  if (vk.size() > vl1[v1.size()] + vl2[vv.size()]) {
    puts("-1");
    return 0;
  }
  re int nw = v1.size(), bs = 1 << 30;
  for (re int i = 0; i <= vv.size(); i++) {
    while (nw != 0 && vl2[i] + vl1[nw - 1] >= vk.size())
      nw--;
    if (vl2[i] + vl1[nw] >= vk.size()) {
      bs = std::min(bs, i + nw * 2);
    }
  }
  printf("%d\\n", ans + bs);
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define re register
struct eg {
  int to, c;
  eg *nx;
} e[200100], *la[100100], *cnt = e;
inline void addE(re int a, re int b, re int c) {
  *++cnt = (eg){b, c, la[a]};
  la[a] = cnt;
  *++cnt = (eg){a, c, la[b]};
  la[b] = cnt;
}
int d[100100], dd[100100], dd1[100100], aa[100100], bb[100100], cc[100100], bl;
long long num, nc;
bool vis[200100];
std::vector<long long> vv, vk;
void dfs(re int a) {
  vis[a] = 1;
  if (d[a] > 2)
    bl = 1;
  for (re eg *i = la[a]; i; i = i->nx) {
    if (!vis[i->to]) {
      dfs(i->to);
    }
    nc += i->c - 1;
  }
}
int main() {
  freopen("01-06.txt", "r", stdin);
  re int n, m, ans = 0, a, b, c;
  scanf("%d%d", &n, &m);
  for (re int i = 1; i <= m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    aa[i] = a;
    bb[i] = b;
    cc[i] = c;
    if (a == b) {
      d[a] += 3;
    } else {
      d[a]++;
      d[b]++;
      addE(a, b, c);
      dd[b]++;
      dd1[a]++;
      ans++;
    }
  }
  for (re int i = 1; i <= m; i++) {
    if (aa[i] == bb[i]) {
      if (d[aa[i]] != 3 && cc[i] > 1)
        vv.push_back(cc[i] - 1);
    }
  }
  for (re int i = 1; i <= n; i++) {
    if (!dd1[a] && dd[b]) {
      puts("-1");
      return 0;
    }
    if (!vis[i]) {
      bl = 0;
      nc = 0;
      dfs(i);
      nc /= 2; // printf("****%d %d %lld\\n",i,bl,nc);
      if (bl) {
        num += nc; // printf("**a**%d %lld\\n",i,nc);
      } else {
        vk.push_back(nc);
      }
    }
  }
  // printf("****%lld %d %d\\n",num,ans,vk.size());
  std::sort(vv.begin(), vv.end());
  std::sort(vk.begin(), vk.end());
  while (!vk.empty()) {
    if (!num) {
      if (!vv.empty())
        ans++, num += *vv.rbegin(), vv.pop_back();
      else {
        puts("-1");
        return 0;
      }
    }
    num += *vk.rbegin() - 1;
    ans++;
    vk.pop_back();
    //	printf("****%lld %d %d\\n",num,ans,vk.size());
  }
  printf("%d\\n", ans);
}