Skip to content

Instantly share code, notes, and snippets.

@asi1024
Created August 26, 2015 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asi1024/908c52df79e06784a203 to your computer and use it in GitHub Desktop.
Save asi1024/908c52df79e06784a203 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps = 1e-9, pi = acos(-1.0);
typedef int Data;
const int len = 1 << 17;
struct BIT {
vector<Data> data;
BIT() : data(len, 0) {}
void update(int i, Data value) {
for (; i < len; i |= i+1) data[i] += value;
}
Data query(int i) {
Data s = 0;
for (; i >= 0; i = (i&(i+1))-1) s += data[i];
return s;
}
};
vector<int> compressor(vector<ll> c) {
const int n = c.size();
vector<ll> v = c;
vector<int> res(n);
sort(v.begin(), v.end());
auto it = unique(ALL(v));
REP(i,n) res[i] = lower_bound(begin(v), it, c[i]) - begin(v);
return res;
}
int main() {
int N;
cin >> N;
vector<ll> p(N), l(N), sum(N+1);
REP(i,N) {
cin >> p[i];
sum[i+1] = sum[i] + p[i];
}
REP(i,N) cin >> l[i];
BIT bit;
vector<int> v = compressor(sum);
reverse(ALL(v));
ll res = 0;
for (int i: v) {
res += bit.query(i);
bit.update(i, 1);
}
sort(ALL(sum));
bool flag = true;
REP(i,N)
if (sum[i+1] - sum[i] < l[i]) flag = false;
if (flag) cout << res << endl;
else cout << -1 << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment