Problem p03396 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
ll pl[303030], tm[303030];
int main() {
  int num, len;
  scanf("%d%d", &num, &len);
  for (int i = 0; i < num; i++)
    scanf("%lld", &pl[i]);
  ll ad = 0;
  vector<ll> v1, v2;
  set<ll> se;
  ll maxi = 0, v3 = -1;
  for (int i = 0; i < num; i++) {
    ll z;
    scanf("%lld", &z);
    ad += (z - 1) / (len * 2);
    z = (z - 1) % (len * 2) + 1;
    bool f1 = (z <= (len - pl[i]) * 2), f2 = (z <= pl[i] * 2);
    if (f1 && f2)
      se.insert(pl[i]);
    if (f1 && !f2)
      v1.push_back(pl[i]);
    if (!f1 && f2)
      v2.push_back(pl[i]);
    if (!f1 && !f2)
      v3 = pl[i], ad++;
    if (f1 || f2)
      maxi = pl[i];
  }
  if (!v2.empty()) {
    ll ans = v1.size() + v2.size() + 1;
    for (int i = 0; i < v1.size(); i++) {
      set<ll>::iterator it = se.begin();
      if (it != se.end() && (*it) < v1[i])
        se.erase(it);
    }
    v2.push_back(v2[v2.size() - 1]);
    for (int i = v2.size() - 1; i >= 0; i--) {
      set<ll>::iterator it = se.end();
      bool zz = false;
      if (it != se.begin()) {
        it--;
        if (v2[i] < (*it))
          se.erase(it), zz = true;
      }
      if (!zz)
        maxi = len;
    }
    ans += (se.size() + 1) / 2;
    if (se.size() % 2 == 1)
      maxi = len;
    if (maxi < v3)
      ans++;
    printf("%lld\n", (ad + ans) * len * 2);
  } else {
    if (!v1.empty()) {
      for (int i = 0; i < v1.size(); i++) {
        if (se.empty() && v3 < maxi) {
          printf("%lld\n", (ad + v1.size()) * len * 2);
          return 0;
        }
        set<ll>::iterator it = se.begin();
        if (it != se.end() && (*it) < v1[i])
          se.erase(it);
      }
      ll ans = v1.size() + 1;
      ans += se.size() / 2;
      if (se.size() % 2 == 0)
        maxi = len;
      if (maxi < v3)
        ans++;
      printf("%lld\n", (ad + ans) * len * 2);
    } else
      abort();
  }
}

Fixed submission:
#include <algorithm>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
ll pl[303030], tm[303030];
int main() {
  int num, len;
  scanf("%d%d", &num, &len);
  for (int i = 0; i < num; i++)
    scanf("%lld", &pl[i]);
  ll ad = 0;
  vector<ll> v1, v2;
  set<ll> se;
  ll maxi = 0, v3 = -1;
  for (int i = 0; i < num; i++) {
    ll z;
    scanf("%lld", &z);
    ad += (z - 1) / (len * 2);
    z = (z - 1) % (len * 2) + 1;
    bool f1 = (z <= (len - pl[i]) * 2), f2 = (z <= pl[i] * 2);
    if (f1 && f2)
      se.insert(pl[i]);
    if (f1 && !f2)
      v1.push_back(pl[i]);
    if (!f1 && f2)
      v2.push_back(pl[i]);
    if (!f1 && !f2)
      v3 = pl[i], ad++;
    if (f1 || f2)
      maxi = pl[i];
  }
  if (!v2.empty()) {
    ll ans = v1.size() + v2.size() + 1;
    for (int i = 0; i < v1.size(); i++) {
      set<ll>::iterator it = se.begin();
      if (it != se.end() && (*it) < v1[i])
        se.erase(it);
    }
    v2.push_back(v2[v2.size() - 1]);
    for (int i = v2.size() - 1; i >= 0; i--) {
      set<ll>::iterator it = se.end();
      bool zz = false;
      if (it != se.begin()) {
        it--;
        if (v2[i] < (*it))
          se.erase(it), zz = true;
      }
      if (!zz)
        maxi = len;
    }
    ans += (se.size() + 1) / 2;
    if (se.size() % 2 == 1)
      maxi = len;
    if (maxi < v3)
      ans++;
    printf("%lld\n", (ad + ans) * len * 2);
  } else {
    if (!v1.empty()) {
      for (int i = 0; i < v1.size(); i++) {
        if (se.empty() && v3 < maxi) {
          printf("%lld\n", (ad + v1.size()) * len * 2);
          return 0;
        }
        set<ll>::iterator it = se.begin();
        if (it != se.end() && (*it) < v1[i])
          se.erase(it);
      }
      ll ans = v1.size() + 1;
      ans += se.size() / 2;
      if (se.size() % 2 == 0)
        maxi = len;
      if (maxi < v3)
        ans++;
      printf("%lld\n", (ad + ans) * len * 2);
    } else {
      ll ans = 1;
      ans += se.size() / 2;
      if (se.size() % 2 == 0)
        maxi = len;
      if (maxi < v3)
        ans++;
      printf("%lld\n", (ad + ans) * len * 2);
    }
  }
}