Problem p03982 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

int main() {
  static ll n;
  static ll a[100010], b[100010];
  scanf("%lld", &n);
  rep(i, n) scanf("%lld", &a[i]);
  rep(i, n) scanf("%lld", &b[i]);

  ll loc, p;
  priority_queue<ll, vector<ll>, greater<ll>> que;
  rep(i, 100010) que.push(0);
  loc = 0;
  p = a[0];
  ll sum = 0;
  ll ret = 0;
  int k = 0;
  rep(i, n - 1) {
    sum += b[i];
    ll x = sum - p;
    que.push(x);
    que.push(x);
    ret += x - que.top();
    loc = que.top() + p;
    que.pop();
    p += a[i + 1];
  }
  sum += b[n - 1];
  ll t = 0;
  while (que.top() + p < sum) {
    ret += t * (que.top() + p - loc);
    loc = que.top() + p;
    t++;
  }
  ret += t * max((ll)0, sum - loc);
  cout << ret << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

int main() {
  static ll n;
  static ll a[100010], b[100010];
  scanf("%lld", &n);
  rep(i, n) scanf("%lld", &a[i]);
  rep(i, n) scanf("%lld", &b[i]);

  ll loc, p;
  priority_queue<ll, vector<ll>, greater<ll>> que;
  rep(i, 100010) que.push(0);
  loc = 0;
  p = a[0];
  ll sum = 0;
  ll ret = 0;
  int k = 0;
  rep(i, n - 1) {
    sum += b[i];
    ll x = sum - p;
    que.push(x);
    que.push(x);
    ret += x - que.top();
    loc = que.top() + p;
    que.pop();
    p += a[i + 1];
  }
  sum += b[n - 1];
  ll t = 0;
  while (que.top() + p < sum) {
    ret += t * (que.top() + p - loc);
    loc = que.top() + p;
    que.pop();
    t++;
  }
  ret += t * max((ll)0, sum - loc);
  cout << ret << endl;
}