#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<ll> vi;
int main(){
	ios_base::sync_with_stdio(false);
	ll n;cin>>n;
	vi v(n),t(n);
	for(ll i=0;i<n;i++)
		cin>>v[i];
	for(ll i=0;i<n;i++)
		cin>>t[i];
	set<ll> s;
	ll sum=0;
	set<ll> ::iterator itr;
	map<ll,ll> mp;
	//cout<<p<<endl;
	set<ll> snow;
	ll rm=0;
	for(ll i=0;i<n;i++){
		snow.insert(v[i]+rm);
	    long long now=t[i]*(long long) snow.size();
	    while (!snow.empty()) {
	      long long x = *snow.begin();
	      if (x - rm - t[i] <= 0) {
	        now -= t[i];
	        now += x - rm;
	        snow.erase(snow.begin());
	      } else break;
	    }
	    rm +=t[i];
	    cout << now << ' ';
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<ll> vi;
int main(){
	ios_base::sync_with_stdio(false);
	ll n;cin>>n;
	vi v(n),t(n);
	for(ll i=0;i<n;i++)
		cin>>v[i];
	for(ll i=0;i<n;i++)
		cin>>t[i];
	set<ll> s;
	ll sum=0;
	ll p=0;
	while(p<n && v[p]<t[p]){
		cout<<v[p]<<" ";
		p++;
	}
	set<ll> ::iterator itr;
	map<ll,ll> mp;
	//cout<<p<<endl;
	set<ll> s1;
	for(ll i=p;i<n;i++){
		ll sum=0;
		s.insert(v[i]);
		mp[v[i]]++;
		if(t[i]==0){
			cout<<"0 ";
			continue;
		}
		itr=s.begin();
		while(itr!=s.end()){
			//cout<<*itr<<" ";
			if(*itr<=t[i]){
				sum=sum+((*itr)*mp[*itr]);
				mp[*itr]=0;
			}
			else{
				sum=sum+((t[i])*mp[*itr]);
				mp[*itr-t[i]]=mp[*itr];
				mp[*itr]=0;
				s1.insert(*itr-t[i]);
				//cout<<*itr-t[i]<<" "<<mp[*itr-t[i]]<<endl;
			}
			++itr;
		}
		s.clear();
		s=s1;
		s1.clear();
		cout<<sum<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef vector<ll> vi;
int main(){
	ios_base::sync_with_stdio(false);
	ll n;cin>>n;
	vi v(n),t(n);
	for(ll i=0;i<n;i++)
		cin>>v[i];
	for(ll i=0;i<n;i++)
		cin>>t[i];
	set<ll> s;
	ll sum=0;
	set<ll> ::iterator itr;
	map<ll,ll> mp;
	//cout<<p<<endl;
	multiset<ll> snow;
	ll rm=0;
	for(ll i=0;i<n;i++){
		snow.insert(v[i]+rm);
	    long long now=t[i]*(long long) snow.size();
	    while (!snow.empty()) {
	      long long x=*snow.begin();
	      if (x - rm - t[i] <= 0) {
	        now -= t[i];
	        now += x - rm;
	        snow.erase(snow.begin());
	      } else break;
	    }
	    rm +=t[i];
	    cout << now << ' ';
	}
	return 0;
}


#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define INF 1e9
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    SPEED;

    int n;
    cin >> n;
    vector<int> V(n), T(n);
    for (int i = 0; i < n; i++)
        cin >> V[i];
    vector<int> prefT(n);
    for (int i = 0; i < n; i++) {
            cin >> T[i];
            if (i > 0) {
                prefT[i] = prefT[i-1] + T[i];
                V[i] += prefT[i-1];
            } else
                prefT[0] = T[0];
    }

    vector<int> fin(n), plus(n);
    for (int i = 0; i < n; i++) {
        auto it = lower_bound(prefT.begin(), prefT.end(), V[i]);
        fin[i] = it - prefT.begin();
        if (fin[i] != n)
            plus[fin[i]] += V[i] - *(--it);
    }

    sort(fin.begin(), fin.end());
    for (int i = 0; i < n; i++) {
        cout << (fin.end() - upper_bound(fin.begin(), fin.end(), i) - n + i + 1) * T[i] + plus[i] << " ";
    }

    return 0;
}


#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define INF 1e9
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    SPEED;

    int n;
    cin >> n;
    vector<int> V(n), T(n);
    for (int i = 0; i < n; i++)
        cin >> V[i];
    vector<ll> prefT(n);
    for (int i = 0; i < n; i++) {
            cin >> T[i];
            if (i > 0) {
                prefT[i] = prefT[i-1] + T[i];
                V[i] += prefT[i-1];
            } else
                prefT[0] = T[0];
    }

    multiset<int> have;
    for (int i = 0; i < n; i++) {
        have.insert(V[i]);
        ll res = 0;
        for (auto it = have.begin(); it != have.end() && *it <= prefT[i]; ) {
            if (i > 0)
                res += *it - prefT[i-1];
            else
                res += *it;
            it++;
            have.erase(--have.find(*it));
        }
        res += have.size() * T[i];
        cout << res << " ";
    }

    return 0;
}


#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define INF 1e9
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    SPEED;

    int n;
    cin >> n;
    vector<ll> V(n), T(n);
    for (int i = 0; i < n; i++)
        cin >> V[i];
    vector<ll> prefT(n);
    for (int i = 0; i < n; i++) {
            cin >> T[i];
            if (i > 0) {
                prefT[i] = prefT[i-1] + T[i];
                V[i] += prefT[i-1];
            } else
                prefT[0] = T[0];
    }

    multiset<ll> have;
    for (int i = 0; i < n; i++) {
        have.insert(V[i]);
        ll res = 0;
        for (auto it = have.begin(); it != have.end() && *it <= prefT[i]; ) {
            if (i > 0)
                res += *it - prefT[i-1];
            else
                res += *it;
            it++;
            have.erase(--have.find(*it));
        }
        res += have.size() * T[i];
        cout << res << " ";
    }

    return 0;
}


#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define INF 1e9
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    SPEED;

    int n;
    cin >> n;
    vector<int> V(n), T(n);
    for (int i = 0; i < n; i++)
        cin >> V[i];
    vector<ll> prefT(n);
    for (int i = 0; i < n; i++) {
            cin >> T[i];
            if (i > 0) {
                prefT[i] = prefT[i-1] + T[i];
                V[i] += prefT[i-1];
            } else
                prefT[0] = T[0];
    }

    multiset<ll> have;
    for (int i = 0; i < n; i++) {
        have.insert(V[i]);
        ll res = 0;
        for (auto it = have.begin(); it != have.end() && *it <= prefT[i]; ) {
            if (i > 0)
                res += *it - prefT[i-1];
            else
                res += *it;
            it++;
            have.erase(--have.find(*it));
        }
        res += have.size() * T[i];
        cout << res << " ";
    }

    return 0;
}


#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define INF 1e9
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    SPEED;

    int n;
    cin >> n;
    vector<int> V(n), T(n);
    for (int i = 0; i < n; i++)
        cin >> V[i];
    vector<int> prefT(n);
    for (int i = 0; i < n; i++) {
            cin >> T[i];
            if (i > 0) {
                prefT[i] = prefT[i-1] + T[i];
                V[i] += prefT[i-1];
            } else
                prefT[0] = T[0];
    }

    vector<int> fin(n), plus(n);
    for (int i = 0; i < n; i++) {
        if (i > 0 && V[i] == prefT[i-1]) {
            fin[i] = i;
        } else {
            auto it = lower_bound(prefT.begin(), prefT.end(), V[i]);
            fin[i] = it - prefT.begin();
            if (fin[i] != n)
                plus[fin[i]] += V[i] - *(--it);
        }
    }

    sort(fin.begin(), fin.end());
    for (int i = 0; i < n; i++) {
        cout << (fin.end() - upper_bound(fin.begin(), fin.end(), i) - n + i + 1) * T[i] + plus[i] << " ";
    }
    //cout << (fin.end() - upper_bound(fin.begin(), fin.end(), 0) - n + 1);

    return 0;
}


#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define INF 1e9
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    SPEED;

    int n;
    cin >> n;
    vector<int> V(n), T(n);
    for (int i = 0; i < n; i++)
        cin >> V[i];
    vector<ll> prefT(n);
    for (int i = 0; i < n; i++) {
            cin >> T[i];
            if (i > 0) {
                prefT[i] = prefT[i-1] + T[i];
                V[i] += prefT[i-1];
            } else
                prefT[0] = T[0];
    }

    vector<ll> fin(n), plus(n);
    for (int i = 0; i < n; i++) {
        if (i > 0 && V[i] == prefT[i-1]) {
            fin[i] = i;
        } else {
            auto it = lower_bound(prefT.begin(), prefT.end(), V[i]);
            fin[i] = it - prefT.begin();
            if (fin[i] == 0)
                plus[0] = V[0];
            else if (fin[i] != n)
                plus[fin[i]] += V[i] - *(--it);
        }
    }

    sort(fin.begin(), fin.end());
    for (int i = 0; i < n; i++) {
        cout << (fin.end() - upper_bound(fin.begin(), fin.end(), i) - n + i + 1) * T[i] + plus[i] << " ";
    }

    return 0;
}


#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define INF 1e9
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    SPEED;

    int n;
    cin >> n;
    vector<int> V(n), T(n);
    for (int i = 0; i < n; i++)
        cin >> V[i];
    vector<ll> prefT(n);
    for (int i = 0; i < n; i++) {
            cin >> T[i];
            if (i > 0) {
                prefT[i] = prefT[i-1] + T[i];
                V[i] += prefT[i-1];
            } else
                prefT[0] = T[0];
    }

    multiset<int> have;
    for (int i = 0; i < n; i++) {
        have.insert(V[i]);
        ll res = 0;
        for (auto it = have.begin(); it != have.end() && *it <= prefT[i]; it++) {
            if (i > 0)
                res += *it - prefT[i-1];
            else
                res += *it;
            have.erase(have.find(*it));
        }
        res += have.size() * T[i];
        cout << res << " ";
    }

    return 0;
}


#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define INF 1e9
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    SPEED;

    int n;
    cin >> n;
    vector<ll> V(n), T(n);
    for (int i = 0; i < n; i++)
        cin >> V[i];
    //vector<ll> prefT(n);
    for (int i = 0; i < n; i++) {
            cin >> T[i];
            /*
            if (i > 0) {
                prefT[i] = prefT[i-1] + T[i];
                V[i] += prefT[i-1];
            } else
                prefT[0] = T[0];
            */
    }
    ll plus = 0;
    multiset<ll> have;
    for (int i = 0; i < n; i++) {
        have.insert(V[i] + plus);
        ll res = 0;
        for (auto it = have.begin(); it != have.end() && *it <= T[i] + plus; ) {
            res += *it - plus;
            have.erase(it);
            it = have.begin();
        }
        plus += T[i];
        res += have.size() * T[i];
        cout << res << " ";
    }

    return 0;
}


#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define INF 1e9
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    SPEED;

    int n;
    cin >> n;
    vector<int> V(n), T(n);
    for (int i = 0; i < n; i++)
        cin >> V[i];
    vector<ll> prefT(n);
    for (int i = 0; i < n; i++) {
            cin >> T[i];
            if (i > 0) {
                prefT[i] = prefT[i-1] + T[i];
                V[i] += prefT[i-1];
            } else
                prefT[0] = T[0];
    }

    multiset<int> have;
    for (int i = 0; i < n; i++) {
        have.insert(V[i]);
        ll res = 0;
        for (auto it = have.begin(); it != have.end() && *it <= prefT[i]; it++) {
            if (*it - prefT[i-1] >= 0)
                res += *it - prefT[i-1];
            else
                res += *it;
            have.erase(have.find(*it));
        }
        res += have.size() * T[i];
        cout << res << " ";
    }

    return 0;
}


#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define INF 1e9
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    SPEED;

    int n;
    cin >> n;
    vector<int> V(n), T(n);
    for (int i = 0; i < n; i++)
        cin >> V[i];
    vector<int> prefT(n);
    for (int i = 0; i < n; i++) {
            cin >> T[i];
            if (i > 0) {
                prefT[i] = prefT[i-1] + T[i];
                V[i] += prefT[i-1];
            } else
                prefT[0] = T[0];
    }

    vector<int> fin(n), plus(n);
    for (int i = 0; i < n; i++) {
        if (i > 0 && V[i] == prefT[i-1]) {
            fin[i] = i;
        } else {
            auto it = lower_bound(prefT.begin(), prefT.end(), V[i]);
            fin[i] = it - prefT.begin();
            if (fin[i] == 0)
                plus[0] = V[0];
            else if (fin[i] != n)
                plus[fin[i]] += V[i] - *(--it);
        }
    }

    sort(fin.begin(), fin.end());
    for (int i = 0; i < n; i++) {
        cout << (fin.end() - upper_bound(fin.begin(), fin.end(), i) - n + i + 1) * T[i] + plus[i] << " ";
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct node {
	long long data, y, summ, add,size,c;
	node* left;
	node* right;
	node(long long val){
		data = val;
		y = (rand() << 15) + rand();
		summ = val;
		add = 0;
		c = 1;
		size = 1;
		left = nullptr;
		right = nullptr;
	}
	~node() {
		if (left) {
			delete left;
		}
		if (right) {
			delete right;
		}
	}
};
long long get_size(node* root) {
	if (!root) {
		return 0;
	}
	return root->size;
}
void push(node* root) {
	if (!root || root->add == 0) return;
	if (root->left) root->left->add += root->add;
	if (root->right) root->right->add += root->add;
	root->summ += root->add * root->size;
	root->data += root->add;
	root->add = 0;
}
long long get_sum(node* root) {
	if (!root) {
		return 0;
	}
	push(root);
	return root->summ;
}
void update(node* root) {
	if (!root) return;
	root->size = get_size(root->left) + get_size(root->right) + root -> c;
	root -> summ = get_sum(root->left) + get_sum(root->right) + (root->data)*(root->c);
}

pair<node*, node*> split(node* root, long long k) {
	if (!root) return { nullptr,nullptr };
	push(root);
	if (root -> data <= k) {
		auto ans = split(root->right, k);
		root->right = ans.first;
		update(root);
		return { root,ans.second };
	}
	else {
		auto ans = split(root->left, k);
		root->left = ans.second;
		update(root);
		return{ ans.first,root };
	}
}
node* merge(node* root1, node* root2) {
	if (!root1) return root2;
	if (!root2) return root1;
	if (root1->y <= root2->y) {
		push(root1);
		root1->right = merge(root1->right, root2);
		update(root1);
		return root1;
	}
	else {
		push(root2);
		root2->left = merge(root1, root2->left);
		update(root2);
		return root2;
	}
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    long long n;
    node*root = nullptr;
    cin >> n;
    vector<long long> p(n);
    vector<long long> p1(n);
    for(long long i = 0; i < n; i ++){
        cin >> p[i];
    }
    for(long long i = 0; i < n; i ++){
        cin >> p1[i];
    }
    for(long long i = 0; i < n; i ++){
        auto ans = split(root,p[i]);
        auto ans1 = split(ans.first,p[i]-1);
        if(ans1.second != nullptr){
            ans1.second -> c += 1;
            ans1.second -> size += 1;
            update(ans1.second);
            root = merge(merge(ans1.first,ans1.second),ans.second);
        }
        else{
            node * neww = new node(p[i]);
            root = merge(merge(ans1.first,neww),ans.second);
        }
        root -> add += -p1[i];
        long long si = get_size(root);
        ans = split(root,0);
        //get_sum(ans.first) - si * p1[i]
        cout << get_sum(ans.first) + si * p1[i] << ' ';
        root = ans.second;
    }
}


#include <bits/stdc++.h>
using namespace std;
struct node {
	long long data, y, summ, add,size,c;
	node* left;
	node* right;
	node(long long val){
		data = val;
		y = (rand() << 15) + rand();
		summ = val;
		add = 0;
		c = 1;
		size = 1;
		left = nullptr;
		right = nullptr;
	}
	~node() {
		if (left) {
			delete left;
		}
		if (right) {
			delete right;
		}
	}
};
long long get_size(node* root) {
	if (!root) {
		return 0;
	}
	return root->size;
}
void push(node* root) {
	if (!root || root->add == 0) return;
	if (root->left) root->left->add += root->add;
	if (root->right) root->right->add += root->add;
	root->summ += root->add * root->size;
	root->data += root->add;
	root->add = 0;
}
long long get_sum(node* root) {
	if (!root) {
		return 0;
	}
	push(root);
	return root->summ;
}
void update(node* root) {
	if (!root) return;
	root->size = get_size(root->left) + get_size(root->right) + root -> c;
	root -> summ = get_sum(root->left) + get_sum(root->right) + root->data*(root->c);
}

pair<node*, node*> split(node* root, long long k) {
	if (!root) return { nullptr,nullptr };
	push(root);
	if (root -> data <= k) {
		auto ans = split(root->right, k);
		root->right = ans.first;
		update(root);
		return { root,ans.second };
	}
	else {
		auto ans = split(root->left, k);
		root->left = ans.second;
		update(root);
		return{ ans.first,root };
	}
}
node* merge(node* root1, node* root2) {
	if (!root1) return root2;
	if (!root2) return root1;
	if (root1->y <= root2->y) {
		push(root1);
		root1->right = merge(root1->right, root2);
		update(root1);
		return root1;
	}
	else {
		push(root2);
		root2->left = merge(root1, root2->left);
		update(root2);
		return root2;
	}
}

int main()
{
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    long long n;
    node*root = nullptr;
    cin >> n;
    vector<long long> p(n);
    vector<long long> p1(n);
    for(long long i = 0; i < n; i ++){
        cin >> p[i];
    }
    for(long long i = 0; i < n; i ++){
        cin >> p1[i];
    }
    for(long long i = 0; i < n; i ++){
        auto ans = split(root,p[i]);
        auto ans1 = split(ans.first,p[i]-1);
        if(ans1.second != nullptr){
            ans1.second -> c += 1;
            ans1.second -> size += 1;
            root = merge(merge(ans1.first,ans1.second),ans.second);
        }
        else{
            node * neww = new node(p[i]);
            root = merge(merge(ans1.first,neww),ans.second);
        }
        root -> add += -p1[i];
        long long si = get_size(root);
        ans = split(root,0);
        //get_sum(ans.first) - si * p1[i]
        cout << get_sum(ans.first) + si * p1[i] << ' ';
        root = ans.second;
    }
}


#include <bits/stdc++.h>
using namespace std;
struct node {
	long long data, y, summ, add,size,c;
	node* left;
	node* right;
	node(long long val){
		data = val;
		y = (rand() << 15) + rand();
		summ = val;
		add = 0;
		c = 1;
		size = 1;
		left = nullptr;
		right = nullptr;
	}
	~node() {
		if (left) {
			delete left;
		}
		if (right) {
			delete right;
		}
	}
};
long long get_size(node* root) {
	if (!root) {
		return 0;
	}
	return root->size;
}
void push(node* root) {
	if (!root || root->add == 0) return;
	if (root->left) root->left->add += root->add;
	if (root->right) root->right->add += root->add;
	root->summ += root->add * root->size;
	root->data += root->add;
	root->add = 0;
}
long long get_sum(node* root) {
	if (!root) {
		return 0;
	}
	push(root);
	return root->summ;
}
void update(node* root) {
	if (!root) return;
	push(root);
	root->size = get_size(root->left) + get_size(root->right) + root -> c;
	root -> summ = get_sum(root->left) + get_sum(root->right) + (root->data)*(root->c);
}

pair<node*, node*> split(node* root, long long k) {
	if (!root) return { nullptr,nullptr };
	push(root);
	if (root -> data <= k) {
		auto ans = split(root->right, k);
		root->right = ans.first;
		update(root);
		return { root,ans.second };
	}
	else {
		auto ans = split(root->left, k);
		root->left = ans.second;
		update(root);
		return{ ans.first,root };
	}
}
node* merge(node* root1, node* root2) {
	if (!root1) return root2;
	if (!root2) return root1;
	if (root1->y <= root2->y) {
		push(root1);
		root1->right = merge(root1->right, root2);
		update(root1);
		return root1;
	}
	else {
		push(root2);
		root2->left = merge(root1, root2->left);
		update(root2);
		return root2;
	}
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    long long n;
    node*root = nullptr;
    cin >> n;
    vector<long long> p(n);
    vector<long long> p1(n);
    for(long long i = 0; i < n; i ++){
        cin >> p[i];
    }
    for(long long i = 0; i < n; i ++){
        cin >> p1[i];
    }
    for(long long i = 0; i < n; i ++){
        auto ans = split(root,p[i]);
        auto ans1 = split(ans.first,p[i]-1);
        if(ans1.second != nullptr){
            ans1.second -> c += 1;
            ans1.second -> size += 1;
            update(ans1.second);
            root = merge(merge(ans1.first,ans1.second),ans.second);
        }
        else{
            node * neww = new node(p[i]);
            root = merge(merge(ans1.first,neww),ans.second);
        }
        root -> add += -p1[i];
        long long si = get_size(root);
        ans = split(root,0);
        //get_sum(ans.first) - si * p1[i]
        cout << get_sum(ans.first) + si * p1[i] << ' ';
        root = ans.second;
    }
}


#include <bits/stdc++.h>
using namespace std;
struct node {
	long long data, y, summ, add,size,c;
	node* left;
	node* right;
	node(long long val){
		data = val;
		y = (rand() << 15) + rand();
		summ = val;
		add = 0;
		c = 1;
		size = 1;
		left = nullptr;
		right = nullptr;
	}
	~node() {
		if (left) {
			delete left;
		}
		if (right) {
			delete right;
		}
	}
};
long long get_size(node* root) {
	if (!root) {
		return 0;
	}
	return root->size;
}
void push(node* root) {
	if (!root || root->add == 0) return;
	if (root->left) root->left->add += root->add;
	if (root->right) root->right->add += root->add;
	root->summ += root->add * root->size;
	root->data += root->add;
	root->add = 0;
}
long long get_sum(node* root) {
	if (!root) {
		return 0;
	}
	push(root);
	return root->summ;
}
void update(node* root) {
	if (!root) return;
	push(root);
	root->size = get_size(root->left) + get_size(root->right) + root -> c;
	root -> summ = get_sum(root->left) + get_sum(root->right) + (root->data)*(root->c);
}

pair<node*, node*> split(node* root, long long k) {
	if (!root) return { nullptr,nullptr };
	push(root);
	if (root -> data <= k) {
		auto ans = split(root->right, k);
		root->right = ans.first;
		update(root);
		return { root,ans.second };
	}
	else {
		auto ans = split(root->left, k);
		root->left = ans.second;
		update(root);
		return{ ans.first,root };
	}
}
node* merge(node* root1, node* root2) {
	if (!root1) return root2;
	if (!root2) return root1;
	if (root1->y <= root2->y) {
		push(root1);
		root1->right = merge(root1->right, root2);
		update(root1);
		return root1;
	}
	else {
		push(root2);
		root2->left = merge(root1, root2->left);
		update(root2);
		return root2;
	}
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    long long n;
    node*root = nullptr;
    cin >> n;
    vector<long long> p(n);
    vector<long long> p1(n);
    for(long long i = 0; i < n; i ++){
        cin >> p[i];
    }
    for(long long i = 0; i < n; i ++){
        cin >> p1[i];
    }
    for(long long i = 0; i < n; i ++){
        auto ans = split(root,p[i]);
        auto ans1 = split(ans.first,p[i]-1);
        if(ans1.second != nullptr){
            ans1.second -> c += 1;
            ans1.second -> size += 1;
            root = merge(merge(ans1.first,ans1.second),ans.second);
        }
        else{
            node * neww = new node(p[i]);
            root = merge(merge(ans1.first,neww),ans.second);
        }
        root -> add += -p1[i];
        long long si = get_size(root);
        ans = split(root,0);
        //get_sum(ans.first) - si * p1[i]
        cout << get_sum(ans.first) + si * p1[i] << ' ';
        root = ans.second;
    }
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI1 3.14159265358979311599796346854418516159057617187500

typedef long long int ll;
ll MOD = 1e9 + 7;

ll power(ll a , ll n)
{
    unsigned long long int result=1,x=a;
    while(n>0)
    {
        if(n % 2 ==1)
            result = (result * x)%MOD;
        x = (x * x)%MOD;
        n=n/2;
    }
    return result;
}

ll GCD(ll a , ll b)
{
    if(b==0)
        return a;
    else
        return GCD(b,a%b);
}

/*ll spf[10000001];
bool vis[10000001];

void sieve()
{
    spf[1] = 1;
    for (int i=2; i<=10000000; i++)
        spf[i] = i;

    for (int i=4; i<=10000000; i+=2)
        spf[i] = 2;

    for (int i=3; i*i<=10000000; i++)
    {
        if (spf[i] == i)
        {
            for(int j=i*i; j<=10000000; j+=i)
                if(spf[j]==j)
                    spf[j] = i;
        }
    }
} */

//int dp[10000000][4];

int main()
{
    FIO
    //cout << (int)prime.size() << endl;
    int t;
    t=1;
    while(t--)
    {
        int n;
        cin >> n;

        ll a[n],b[n];

        for(int i=0;i<n;i++)
            cin >> a[i];

        for(int i=0;i<n;i++)
            cin >> b[i];

        map<int, vector<ll> > mp;

        vector<ll> pre(n);
        pre[0] = b[0];
        for(int i=1;i<n;i++)
            pre[i] = pre[i-1] + b[i];

        ll ans[n],rem[n];
        ll sum = 0;

        for(int i=0;i<n;i++)
        {
            if(i==n-1)
            {
                vector<ll> tem;
                tem = mp[i];
                ll sum1 = sum*b[i];
                for(int j=0;j<tem.size();j++)
                {
                    //cout << pre[i] << " " << rem[tem[j]] << endl;
                    sum1 -= (pre[i] - pre[tem[j]]) - rem[tem[j]];
                    sum--;
                }

                ans[i] = sum1 + min(a[i],b[i]);
                continue;
            }
            rem[i] = max((ll)0,a[i]-b[i]);
            int pp = lower_bound(pre.begin()+i+1,pre.end(),rem[i]+pre[i]) - pre.begin();
            //cout << i << " " << pp << endl;
            vector<ll> tem;
            tem = mp[i];
            //cout << sum << endl;
            ll sum1 = sum*b[i];
            for(int j=0;j<tem.size();j++)
            {
                //cout << pre[i] << " " << rem[tem[j]] << endl;
                sum1 -= (pre[i] - pre[tem[j]]) - rem[tem[j]];
                sum--;
            }
            //cout << sum1 << endl;
            ans[i] = sum1 + min(a[i],b[i]);
            if(rem[i]!=0)
            {
                sum++;
                mp[pp].push_back(i);
            }
        }

        for(int i=0;i<n;i++)
            cout << ans[i] << " ";
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI1 3.14159265358979311599796346854418516159057617187500

typedef long long int ll;
ll MOD = 1e9 + 7;

ll power(ll a , ll n)
{
    unsigned long long int result=1,x=a;
    while(n>0)
    {
        if(n % 2 ==1)
            result = (result * x)%MOD;
        x = (x * x)%MOD;
        n=n/2;
    }
    return result;
}

ll GCD(ll a , ll b)
{
    if(b==0)
        return a;
    else
        return GCD(b,a%b);
}

/*ll spf[10000001];
bool vis[10000001];

void sieve()
{
    spf[1] = 1;
    for (int i=2; i<=10000000; i++)
        spf[i] = i;

    for (int i=4; i<=10000000; i+=2)
        spf[i] = 2;

    for (int i=3; i*i<=10000000; i++)
    {
        if (spf[i] == i)
        {
            for(int j=i*i; j<=10000000; j+=i)
                if(spf[j]==j)
                    spf[j] = i;
        }
    }
} */

//int dp[10000000][4];

int main()
{
    FIO
    //cout << (int)prime.size() << endl;
    int t;
    t=1;
    while(t--)
    {
        int n;
        cin >> n;

        ll a[n],b[n];

        for(int i=0;i<n;i++)
            cin >> a[i];

        for(int i=0;i<n;i++)
            cin >> b[i];

        map<int, vector<ll> > mp;

        vector<ll> pre(n);
        pre[0] = b[0];
        for(int i=1;i<n;i++)
            pre[i] = pre[i-1] + b[i];

        ll ans[n],rem[n];
        ll sum = 0;

        for(int i=0;i<n;i++)
        {
            if(i==n-1)
            {
                vector<ll> tem;
                tem = mp[i];
                ll sum1 = sum*b[i];
                for(int j=0;j<tem.size();j++)
                {
                    //cout << pre[i] << " " << rem[tem[j]] << endl;
                    sum1 -= (pre[i] - pre[tem[j]]) - rem[tem[j]];
                    sum -= (rem[tem[j]]!=0);
                }
                ans[i] = max((ll)0,sum1) + min(a[i],b[i]);
                continue;
            }
            rem[i] = max((ll)0,a[i]-b[i]);
            int pp = lower_bound(pre.begin()+i+1,pre.end(),rem[i]+pre[i]) - pre.begin();
            //cout << i << " " << pp << endl;
            mp[pp].push_back(i);
            vector<ll> tem;
            tem = mp[i];
            //cout << sum << endl;
            ll sum1 = sum*b[i];
            for(int j=0;j<tem.size();j++)
            {
                //cout << pre[i] << " " << rem[tem[j]] << endl;
                sum1 -= (pre[i] - pre[tem[j]]) - rem[tem[j]];
                sum -= (rem[tem[j]]!=0);
            }
            //cout << sum1 << endl;
            ans[i] = max((ll)0,sum1) + min(a[i],b[i]);
            sum += (rem[i]!=0);
        }

        for(int i=0;i<n;i++)
            cout << ans[i] << " ";
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI1 3.14159265358979311599796346854418516159057617187500

typedef long long int ll;
ll MOD = 1e9 + 7;

ll power(ll a , ll n)
{
    unsigned long long int result=1,x=a;
    while(n>0)
    {
        if(n % 2 ==1)
            result = (result * x)%MOD;
        x = (x * x)%MOD;
        n=n/2;
    }
    return result;
}

ll GCD(ll a , ll b)
{
    if(b==0)
        return a;
    else
        return GCD(b,a%b);
}

/*ll spf[10000001];
bool vis[10000001];

void sieve()
{
    spf[1] = 1;
    for (int i=2; i<=10000000; i++)
        spf[i] = i;

    for (int i=4; i<=10000000; i+=2)
        spf[i] = 2;

    for (int i=3; i*i<=10000000; i++)
    {
        if (spf[i] == i)
        {
            for(int j=i*i; j<=10000000; j+=i)
                if(spf[j]==j)
                    spf[j] = i;
        }
    }
} */

//int dp[10000000][4];

int main()
{
    FIO
    //cout << (int)prime.size() << endl;
    int t;
    t=1;
    while(t--)
    {
        int n;
        cin >> n;

        ll a[n],b[n];

        for(int i=0;i<n;i++)
            cin >> a[i];

        for(int i=0;i<n;i++)
            cin >> b[i];

        map<int, vector<ll> > mp;

        vector<ll> pre(n);
        pre[0] = b[0];
        for(int i=1;i<n;i++)
            pre[i] = pre[i-1] + b[i];

        ll ans[n],rem[n];
        ll sum = 0;

        for(int i=0;i<n;i++)
        {
            if(i==n-1)
            {
                vector<ll> tem;
                tem = mp[i];
                ll sum1 = sum*b[i];
                for(int j=0;j<tem.size();j++)
                {
                    //cout << pre[i] << " " << rem[tem[j]] << endl;
                    sum1 -= max((ll)0,((pre[i] - pre[tem[j]]) - rem[tem[j]]));
                    sum -= (rem[tem[j]]!=0);
                }
                ans[i] = sum1 + min(a[i],b[i]);
                continue;
            }
            rem[i] = max((ll)0,a[i]-b[i]);
            int pp = lower_bound(pre.begin()+i+1,pre.end(),rem[i]+pre[i]) - pre.begin();
            //cout << i << " " << pp << endl;
            mp[pp].push_back(i);
            vector<ll> tem;
            tem = mp[i];
            //cout << sum << endl;
            ll sum1 = sum*b[i];
            for(int j=0;j<tem.size();j++)
            {
                //cout << pre[i] << " " << rem[tem[j]] << endl;
                sum1 -= max((ll)0,((pre[i] - pre[tem[j]]) - rem[tem[j]]));
                sum -= (rem[tem[j]]!=0);
            }
            //cout << sum1 << endl;
            ans[i] = sum1 + min(a[i],b[i]);
            sum += (rem[i]!=0);
        }

        for(int i=0;i<n;i++)
            cout << ans[i] << " ";
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, m, l, a[1000000], b[100000], ans;
set < pair<int,int> > st;
int main() {
	cin>>n;
	for (int i = 0; i < n; i ++) cin>>a[i];
	for (int i = 0; i < n; i ++) cin>>b[i];
	for (int i = 0; i < n; i ++) {
		st.insert({a[i]+l,i});
		l += b[i]; ans = 0;
//		x = (*st.begin()).first;
		while ((*st.begin()).first <= l && st.size()) {
			ans += (*st.begin()).first - (l - b[i]);
			st.erase(st.begin()++);
		}
		ans += st.size() * b[i];
		cout<<ans<<" ";
	}
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, m, l, a[100005], b[100005], ans;
set < pair<ll,ll> > st;
int main() {
	cin>>n;
	for (int i = 0; i < n; i ++) cin>>a[i];
	for (int i = 0; i < n; i ++) cin>>b[i];
	for (int i = 0; i < n; i ++) {
		st.insert({a[i]+l,i});
		l += b[i]; ans = 0;
		while ((*st.begin()).first <= l && st.size()) {
			ans += (*st.begin()).first - l + b[i];
			st.erase(st.begin());
		}
		ans += st.size() * b[i];
		cout<<ans<<" ";
	}
}


#include <bits/stdc++.h>
using namespace std;
int a[100000], b[100000], n, m, x, ans, k;
int main() {
	cin>>n;
	for (int i = 0; i < n; i ++) cin>>a[i];
	for (int i = 0; i < n; i ++) cin>>b[i];
	x = n;
	while (x) {
		for (int i = 0; i < n; i ++) {
			ans = 0;
			for (int j = 0; j <= i; j ++) {
				if (a[j] > 0) {
					if (a[j] <= b[i]) ans += a[j];
					else ans += b[i];
					a[j] -= b[i];
					if (a[j] <= 0) x--;
				}
			}
			if (k < n) cout<<ans<<" ", k++;
		}
	}
}


#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define FF first
#define SS second
#define pii pair<int,int>
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define sz(x) (int)x.size()
#define mp make_pair
#define Mod 1000000007
typedef long long int ll;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);

	int n, i, low, high, mid;
	cin >> n;
	vector<int> volume(n, 0);
	vector<int> temp(n, 0);
	vector<int> prefix;
	std::vector<int> f(n, 0), m(n, 0);

	for(i = 0 ; i < n; i++)
		cin >> volume[i];
	for(i = 0; i < n; i++)
		cin >> temp[i];
	prefix.eb(temp[0]);
	for(i = 1; i < n; i++)
		prefix[i] = prefix[i-1] + temp[i];

	for(i = 0; i < n; i++){
		low = i;
		high = n-1;
		while(low <= high){
			mid = high - (high - low)/2;
			if((prefix[mid] - prefix[i-1]) > volume[i])
				high = mid-1;
			else
				low = mid+1;
		}
		f[i]++;
		if(low < n){
			f[low]--;
			m[low]+=(volume[i] - (prefix[low-1] - prefix[i-1]));
		}
	}

	for(i = 0; i < n; i++)
		f[i] += f[i-1];

	for(i = 0; i < n; i++)
		cout<<(f[i]*temp[i] + m[i])<<" ";
	cout<<"\n";
}


#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define FF first
#define SS second
#define pii pair<int,int>
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define sz(x) (int)x.size()
#define mp make_pair
#define Mod 1000000007
typedef long long int ll;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);

	int n, i, low, high, mid, sub;
	cin >> n;
	vector<int> volume(n, 0);
	vector<int> temp(n, 0);
	vector<int> prefix;
	std::vector<int> f(n, 0), m(n, 0);

	for(i = 0 ; i < n; i++)
		cin >> volume[i];
	for(i = 0; i < n; i++)
		cin >> temp[i];
	prefix.eb(temp[0]);
	for(i = 1; i < n; i++)
		prefix.eb(prefix[i-1] + temp[i]);

	for(i = 0; i < n; i++){
		low = i;
		high = n-1;
		while(low <= high){
			mid = high - (high - low)/2;
			sub = i>0 ? prefix[i-1] : 0;
			if((prefix[mid] - sub) > volume[i])
				high = mid-1;
			else
				low = mid+1;
		}
		f[i]++;
		if(low < n){
			f[low]--;
			m[low]+=(volume[i] - (prefix[low-1] - sub));
		}
	}

	for(i = 0; i < n; i++)
		f[i] += f[i-1];

	for(i = 0; i < n; i++)
		cout<<(f[i]*temp[i] + m[i])<<" ";
	cout<<"\n";
}


	#include "bits/stdc++.h"
	using namespace std;
	#define pb push_back
	#define eb emplace_back
	#define fi first
	#define se second
	#define FF first
	#define SS second
	#define pii pair<int,int>
	#define fr(i, a, b) for(int i = a; i <= b; i++)
	#define sz(x) (int)x.size()
	#define mp make_pair
	#define Mod 1000000007
	typedef long long int ll;

	int main(){
		ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		cout.tie(NULL);

		int n, i, low, high, mid;
		ll sub;
		cin >> n;
		vector<int> volume(n, 0);
		vector<int> temp(n, 0);
		vector<ll> prefix(n, 0);
		std::vector<int> f(n, 0);
		std::vector<ll> m(n, 0);

		for(i = 0 ; i < n; i++)
			cin >> volume[i];
		for(i = 0; i < n; i++)
			cin >> temp[i];
		prefix[0] = temp[0];
		for(i = 1; i < n; i++)
			prefix[i] = prefix[i-1] + temp[i];

		for(i = 0; i < n; i++){
			low = i;
			high = n-1;
			sub = i>0 ? prefix[i-1] : 0;
			while(low <= high){
				mid = high - (high - low)/2;
				if((prefix[mid] - sub) > volume[i])
					high = mid-1;
				else
					low = mid+1;
			}
			f[i]++;
			if(low < n){
				f[low]--;
				m[low]+=(volume[i] - ((low > 0 ? prefix[low-1] : 0) - sub));
			}
		}

		for(i = 1; i < n; i++)
			f[i] += f[i-1];

		for(i = 0; i < n; i++)
			cout<<(f[i]*temp[i] + m[i])<<" ";
		cout<<"\n";
	}


	#include "bits/stdc++.h"
	using namespace std;
	#define pb push_back
	#define eb emplace_back
	#define fi first
	#define se second
	#define FF first
	#define SS second
	#define pii pair<int,int>
	#define fr(i, a, b) for(int i = a; i <= b; i++)
	#define sz(x) (int)x.size()
	#define mp make_pair
	#define Mod 1000000007
	typedef long long int ll;

	int main(){
		ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		cout.tie(NULL);

		int n, i, low, high, mid, sub;
		cin >> n;
		vector<int> volume(n, 0);
		vector<int> temp(n, 0);
		vector<int> prefix(n, 0);
		std::vector<int> f(n, 0), m(n, 0);

		for(i = 0 ; i < n; i++)
			cin >> volume[i];
		for(i = 0; i < n; i++)
			cin >> temp[i];
		prefix[0] = temp[0];
		for(i = 1; i < n; i++)
			prefix[i] = prefix[i-1] + temp[i];

		for(i = 0; i < n; i++){
			low = i;
			high = n-1;
			while(low <= high){
				mid = high - (high - low)/2;
				sub = i>0 ? prefix[i-1] : 0;
				if((prefix[mid] - sub) > volume[i])
					high = mid-1;
				else
					low = mid+1;
			}
			f[i]++;
			if(low < n){
				f[low]--;
				m[low]+=(volume[i] - ((low > 0 ? prefix[low-1] : 0) - sub));
			}
		}

		for(i = 1; i < n; i++)

			f[i] += f[i-1];

		for(i = 0; i < n; i++)
			cout<<(f[i]*temp[i] + m[i])<<" ";
		cout<<"\n";
	}


#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define FF first
#define SS second
#define pii pair<int,int>
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define sz(x) (int)x.size()
#define mp make_pair
#define Mod 1000000007
typedef long long int ll;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);

	int n, i, low, high, mid;
	cin >> n;
	vector<int> volume(n, 0);
	vector<int> temp(n, 0);
	vector<int> prefix;
	std::vector<int> f(n, 0), m(n, 0);

	for(i = 0 ; i < n; i++)
		cin >> volume[i];
	for(i = 0; i < n; i++)
		cin >> temp[i];
	prefix.eb(temp[0]);
	for(i = 1; i < n; i++)
		prefix.eb(prefix[i-1] + temp[i]);

	for(i = 0; i < n; i++){
		low = i;
		high = n-1;
		while(low <= high){
			mid = high - (high - low)/2;
			if((prefix[mid] - prefix[i-1]) > volume[i])
				high = mid-1;
			else
				low = mid+1;
		}
		f[i]++;
		if(low < n){
			f[low]--;
			m[low]+=(volume[i] - (prefix[low-1] - prefix[i-1]));
		}
	}

	for(i = 0; i < n; i++)
		f[i] += f[i-1];

	for(i = 0; i < n; i++)
		cout<<(f[i]*temp[i] + m[i])<<" ";
	cout<<"\n";
}


	#include "bits/stdc++.h"
	using namespace std;
	#define pb push_back
	#define eb emplace_back
	#define fi first
	#define se second
	#define FF first
	#define SS second
	#define pii pair<int,int>
	#define fr(i, a, b) for(int i = a; i <= b; i++)
	#define sz(x) (int)x.size()
	#define mp make_pair
	#define Mod 1000000007
	typedef long long int ll;

	int main(){
		ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		cout.tie(NULL);

		int n, i, low, high, mid, sub;
		cin >> n;
		vector<int> volume(n, 0);
		vector<int> temp(n, 0);
		vector<int> prefix;
		std::vector<int> f(n, 0), m(n, 0);

		for(i = 0 ; i < n; i++)
			cin >> volume[i];
		for(i = 0; i < n; i++)
			cin >> temp[i];
		prefix.eb(temp[0]);
		for(i = 1; i < n; i++)
			prefix.eb(prefix[i-1] + temp[i]);

		for(i = 0; i < n; i++){
			low = i;
			high = n-1;
			while(low <= high){
				mid = high - (high - low)/2;
				sub = i>0 ? prefix[i-1] : 0;
				if((prefix[mid] - sub) > volume[i])
					high = mid-1;
				else
					low = mid+1;
			}
			f[i]++;
			if(low < n){
				f[low]--;
				m[low]+=(volume[i] - (prefix[low-1] - sub));
			}
		}

		for(i = 1; i < n; i++)

			f[i] += f[i-1];

		for(i = 0; i < n; i++)
			cout<<(f[i]*temp[i] + m[i])<<" ";
		cout<<"\n";
	}


	#include "bits/stdc++.h"
	using namespace std;
	#define pb push_back
	#define eb emplace_back
	#define fi first
	#define se second
	#define FF first
	#define SS second
	#define pii pair<int,int>
	#define fr(i, a, b) for(int i = a; i <= b; i++)
	#define sz(x) (int)x.size()
	#define mp make_pair
	#define Mod 1000000007
	typedef long long int ll;

	int main(){
		ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		cout.tie(NULL);

		int n, i, low, high, mid, sub;
		cin >> n;
		vector<int> volume(n, 0);
		vector<int> temp(n, 0);
		vector<int> prefix(n, 0);
		std::vector<int> f(n, 0), m(n, 0);

		for(i = 0 ; i < n; i++)
			cin >> volume[i];
		for(i = 0; i < n; i++)
			cin >> temp[i];
		prefix[0] = temp[0];
		for(i = 1; i < n; i++)
			prefix[i] = prefix[i-1] + temp[i];

		for(i = 0; i < n; i++){
			low = i;
			high = n-1;
			while(low <= high){
				mid = high - (high - low)/2;
				sub = i>0 ? prefix[i-1] : 0;
				if((prefix[mid] - sub) > volume[i])
					high = mid-1;
				else
					low = mid+1;
			}
			f[i]++;
			if(low < n){
				f[low]--;
				m[low]+=(volume[i] - (prefix[low-1] - sub));
			}
		}

		for(i = 1; i < n; i++)

			f[i] += f[i-1];

		for(i = 0; i < n; i++)
			cout<<(f[i]*temp[i] + m[i])<<" ";
		cout<<"\n";
	}


	#include "bits/stdc++.h"
	using namespace std;
	#define pb push_back
	#define eb emplace_back
	#define fi first
	#define se second
	#define FF first
	#define SS second
	#define pii pair<int,int>
	#define fr(i, a, b) for(int i = a; i <= b; i++)
	#define sz(x) (int)x.size()
	#define mp make_pair
	#define Mod 1000000007
	typedef long long int ll;

	int main(){
		ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		cout.tie(NULL);

		int n, i, low, high, mid;
		ll sub;
		cin >> n;
		vector<int> volume(n, 0);
		vector<ll> temp(n, 0);
		vector<ll> prefix(n, 0);
		std::vector<int> f(n, 0);
		std::vector<ll> m(n, 0);

		for(i = 0 ; i < n; i++)
			cin >> volume[i];
		for(i = 0; i < n; i++)
			cin >> temp[i];
		prefix[0] = temp[0];
		for(i = 1; i < n; i++)
			prefix[i] = prefix[i-1] + temp[i];

		for(i = 0; i < n; i++){
			low = i;
			high = n-1;
			sub = i>0 ? prefix[i-1] : 0;
			while(low <= high){
				mid = high - (high - low)/2;
				if((prefix[mid] - sub) > volume[i])
					high = mid-1;
				else
					low = mid+1;
			}
			f[i]++;
			if(low < n){
				f[low]--;
				m[low]+=(volume[i] - ((low > 0 ? prefix[low-1] : 0) - sub));
			}
		}

		for(i = 1; i < n; i++)
			f[i] += f[i-1];

		for(i = 0; i < n; i++)
			cout<<((ll)f[i]*temp[i] + m[i])<<" ";
		cout<<"\n";
	}


	#include "bits/stdc++.h"
	using namespace std;
	#define pb push_back
	#define eb emplace_back
	#define fi first
	#define se second
	#define FF first
	#define SS second
	#define pii pair<int,int>
	#define fr(i, a, b) for(int i = a; i <= b; i++)
	#define sz(x) (int)x.size()
	#define mp make_pair
	#define Mod 1000000007
	typedef long long int ll;

	int main(){
		ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		cout.tie(NULL);

		int n, i, low, high, mid;
		ll sub;
		cin >> n;
		vector<int> volume(n, 0);
		vector<int> temp(n, 0);
		vector<ll> prefix(n, 0);
		std::vector<int> f(n, 0);
		std::vector<ll> m(n, 0);

		for(i = 0 ; i < n; i++)
			cin >> volume[i];
		for(i = 0; i < n; i++)
			cin >> temp[i];
		prefix[0] = temp[0];
		for(i = 1; i < n; i++)
			prefix[i] = prefix[i-1] + temp[i];

		for(i = 0; i < n; i++){
			low = i;
			high = n-1;
			sub = i>0 ? prefix[i-1] : 0;
			while(low <= high){
				mid = high - (high - low)/2;
				if((prefix[mid] - sub) > volume[i])
					high = mid-1;
				else
					low = mid+1;
			}
			f[i]++;
			if(low < n){
				f[low]--;
				m[low]+=(volume[i] - ((low > 0 ? prefix[low-1] : 0) - sub));
			}
		}

		for(i = 1; i < n; i++)
			f[i] += f[i-1];

		for(i = 0; i < n; i++)
			cout<<((ll)(f[i]*temp[i]) + m[i])<<" ";
		cout<<"\n";
	}


#include <stdio.h>
#include <vector>

int main(){
	int n;
	scanf("%d",&n);
	std::vector<int>v(n);
	std::vector<int>t(n);
	for(int i=0;i<n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&t[i]);
	}
	std::vector<__int64>ans;
	int d=0;
	for(int i=0;i<n;i++){
		__int64 sum=0;
		for(int j=d;j<=i;j++){
			if(v[j]>0){
				if(t[i]>=v[j]){
					sum+=v[j];
					v[j]=0;
				}else{
					sum+=t[i];
					v[j]-=t[i];
				}
			}
			if(v[d]==0){
				d++;
			}
		}
		ans.push_back(sum);
	}
	for(int i=0;i<ans.size();i++){
		printf("%I64d ",ans[i]);
	}
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main(){
	int n;
	cin>>n;
	vector<int>v(n);
	vector<int>t(n);
	for(int i=0;i<n;i++){
		cin>>v[i];
	}
	for(int i=0;i<n;i++){
		cin>>t[i];
	}
	vector<int>ans;
	for(int i=0;i<n;i++){
		int sum=0;
		for(int j=0;j<=i;j++){
			if(v[j]>0){
				if(t[i]>=v[j]){
					sum+=v[j];
				}else{
					sum+=t[i];
				}
				v[j]-=t[i];
			}
		}
		ans.push_back(sum);
	}
	for(int i=0;i<ans.size();i++){
		cout<<ans[i]<<" ";
	}
	return 0;
}


#include <stdio.h>
#include <vector>

int main(){
	int n;
	scanf("%d",&n);
	std::vector<int>v(n);
	std::vector<int>t(n);
	for(int i=0;i<n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&t[i]);
	}
	std::vector<int>ans;
	for(int i=0;i<n;i++){
		int sum=0;
		for(int j=0;j<=i;j++){
			if(v[j]>0){
				if(t[i]>=v[j]){
					sum+=v[j];
				}else{
					sum+=t[i];
				}
				v[j]-=t[i];
			}
		}
		ans.push_back(sum);
	}
	for(int i=0;i<ans.size();i++){
		printf("%d ",ans[i]);
	}
	return 0;
}


#include <stdio.h>
#include <vector>

int main(){
	int n;
	scanf("%d",&n);
	std::vector<int>v(n);
	std::vector<int>t(n);
	for(int i=0;i<n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&t[i]);
	}
	std::vector<__int64>ans;
	int d=0;
	for(int i=0;i<n;i++){
		__int64 sum=0;
		for(int j=d;j<=i;j++){
			if(v[j]>0){
				if(t[i]>=v[j]){
					sum+=v[j];
					v[j]=0;
				}else{
					sum+=t[i];
					v[j]-=t[i];
				}
			}
			if(v[d]==0){
				d++;
			}
		}
		ans.push_back(sum);
	}
	for(int i=0;i<ans.size();i++){
		printf("%I64d ",ans[i]);
	}

	return 0;
}


#include <stdio.h>
#include <vector>

int main(){
	int n;
	scanf("%d",&n);
	std::vector<int>v(n);
	std::vector<int>t(n);
	for(int i=0;i<n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&t[i]);
	}
	std::vector<__int64>ans;
	int d=0;
	for(int i=0;i<n;i++){
		__int64 sum=0;
		for(int j=d;j<=i;j++){
			if(v[j]>0){
				if(t[i]>=v[j]){
					sum+=v[j];
					v[j]=0;
				}else{
					sum+=t[i];
					v[j]-=t[i];
				}
			}
			if(v[d]==0){
				d++;
			}
		}
		ans.push_back(sum);

	}
	for(int i=0;i<ans.size();i++){
		printf("%I64d ",ans[i]);
	}
	return 0;
}


#include <stdio.h>
#include <vector>
int main(){
	int n;
	scanf("%d",&n);
	std::vector<int>v(n);
	std::vector<int>t(n);
	for(int i=0;i<n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&t[i]);
	}
	std::vector<__int64>ans;
	int d=0;
	for(int i=0;i<n;i++){
		__int64 sum=0;
		for(int j=d;j<=i;j++){
			if(v[j]>0){
				if(t[i]>=v[j]){
					sum+=v[j];
					v[j]=0;
				}else{
					sum+=t[i];
					v[j]-=t[i];
				}
			}
			if(v[d]==0){
				d++;
			}
		}
		ans.push_back(sum);
	}
	for(int i=0;i<ans.size();i++){
		printf("%I64d ",ans[i]);
	}
	return 0;
}


#include <stdio.h>
#include <vector>

int main(){
	int n;
	scanf("%d",&n);
	std::vector<int>v(n);
	std::vector<int>t(n);
	for(int i=0;i<n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&t[i]);
	}
	std::vector<int>ans;
	int d=0;
	for(int i=0;i<n;i++){
		int sum=0;
		for(int j=d;j<=i;j++){
			if(v[j]>0){
				if(t[i]>=v[j]){
					sum+=v[j];
				}else{
					sum+=t[i];
				}
				v[j]-=t[i];
				if(v[d]==0){
					d++;
				}
			}
		}
		ans.push_back(sum);
	}
	for(int i=0;i<ans.size();i++){
		printf("%d ",ans[i]);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template<class A, class B> ostream& operator << (ostream &out, pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template<class A> ostream& operator << (ostream &out, vector<A> &a) {
 	out << "[";
 	forn(i, sz(a)) {
 		if(i) out << ", ";
 		out << a[i];
 	}
 	return out << "]\n";
}

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = INF + 7;
const ld PI = acosl(-1.0);
const ld EPS = 1e-9;

mt19937 rnd(time(NULL));
mt19937_64 rnd64(time(NULL));

const int N = 100043;

int days[N];
li add[N];
li ans[N];
li cnt_add[N];
int t[N];
int n;
int v[N];
li pref_t[N];

void add_range(int x, int y, li val)
{
	add[x] += val;
	add[y] -= val;
}

void affect(int x, int y)
{
	cnt_add[x]++;
	cnt_add[y]--;
}

void print()
{
	li cur = 0;
	int cur_cnt = 0;
	forn(i, n)
	{
		cur_cnt += cnt_add[i];
		cur += add[i];
		printf("%lld ", cur + cur_cnt * 1ll * t[i]);
	}
	puts("");
}

bool read() {
 	if(scanf("%d", &n) != 1)
 		return false;
 	forn(i, n) scanf("%d", &v[i]);
 	forn(i, n) scanf("%d", &t[i]);
 	return true;
}

void solve() {
	forn(i, n) pref_t[i + 1] = pref_t[i] + t[i];
	forn(i, n)
	{
		if(t[i] >= v[i])
		{
			add_range(i, i + 1, v[i]);
			continue;
		}
		int l = i;
		int lf = i;
		int rg = n + 1;
		while(rg - lf > 1)
		{
			int mid = (lf + rg) / 2;
			if(pref_t[mid] - pref_t[i] <= v[i])
				lf = mid;
			else
				rg = mid;
		}
		affect(l, lf);
		int rem = (v[i] - (pref_t[lf] - pref_t[l]));
	//	cerr << i << " " << lf << " " << rem << endl;
		add_range(lf, lf + 1, rem);
	}
	print();
}

int main() {

#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	int tt = clock();
#endif

	srand(time(NULL));
	cout << fixed << setprecision(15);

	while(read()) {

		solve();

#ifdef _DEBUG
		cerr << "TIME = " << clock() - tt << " ms\n";
		tt = clock();
#endif

	}

	return 0;
}


#include <stdio.h>
#include <vector>

int main(){
	int n;
	scanf("%d",&n);
	std::vector<int>v(n);
	std::vector<int>t(n);
	for(int i=0;i<n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&t[i]);
	}
	std::vector<int>ans;
	int d=0;
	for(int i=0;i<n;i++){
		int sum=0;
		for(int j=d;j<=i;j++){
			if(v[j]>0){
				if(t[i]>=v[j]){
					sum+=v[j];
					v[j]=0;
				}else{
					sum+=t[i];
					v[j]-=t[i];
				}
			}
			if(v[d]==0){
				d++;
			}
		}
		ans.push_back(sum);
	}
	for(int i=0;i<ans.size();i++){
		printf("%d ",ans[i]);
	}
	return 0;
}


#include <stdio.h>
#include <vector>

int main(){
	int n;
	scanf("%d",&n);
	std::vector<int>v(n);
	std::vector<int>t(n);
	for(int i=0;i<n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&t[i]);
	}
	std::vector<__int64>ans;
	int d=0;
	for(int i=0;i<n;i++){
		int sum=0;
		for(int j=d;j<=i;j++){
			if(v[j]>0){
				if(t[i]>=v[j]){
					sum+=v[j];
					v[j]=0;
				}else{
					sum+=t[i];
					v[j]-=t[i];
				}
			}
			if(v[d]==0){
				d++;
			}
		}
		ans.push_back(sum);
	}
	for(int i=0;i<ans.size();i++){
		printf("%I64d ",ans[i]);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
	// your code goes here
	ll n;
	cin>>n;
	ll a[n+1];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i+1];
	}
	ll b[n+1];
	vector<ll> pre(n+1);
	pre[0]=0;
	map<ll,ll> m;
	ll out[n+1];
	for(ll j=1;j<=n;j++)
	{
		out[j]=0;
		cin>>b[j];
		pre[j]=b[j];
		if(j)
		{
			pre[j]+=pre[j-1];
		}
	}
	for(ll i=1;i<=n;i++)
	{
		if(a[i]>pre[n]-pre[i-1])
		{
			m[i]++;
			m[n+1]--;
			continue;
		}
		auto it=lower_bound(pre.begin(),pre.end(),pre[i-1]+a[i]);
		ll j=it-pre.begin();
		j;
		m[i]++;
		m[j]--;
		a[i]-=pre[j-1]-pre[i-1];
		out[j]+=a[i];
	}
	ll k=0;
	for(ll i=1;i<=n;i++)
	{
		k+=m[i];
		out[i]+=(k*b[i]);
		cout<<out[i]<<" ";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
	// your code goes here
	ll n;
	cin>>n;
	ll a[n+1];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i+1];
	}
	ll b[n+1];
	vector<ll> pre(n+1);
	pre[0]=0;
	map<ll,ll> m;
	ll out[n+1];
	for(ll j=1;j<=n;j++)
	{
		out[j]=0;
		cin>>b[j];
		pre[j]=b[j];
		if(j)
		{
			pre[j]+=pre[j-1];
		}
	}
	for(ll i=1;i<=n;i++)
	{
		if(a[i]>pre[n]-pre[i-1])
		{
			m[i]++;
			m[n+1]--;
			continue;
		}
		auto it=lower_bound(pre.begin(),pre.end(),pre[i-1]+a[i]);
		ll j=it-pre.begin();
		j;

		if(j<i)
		{
			continue;
		}
		m[i]++;
		m[j]--;
		a[i]-=pre[j-1]-pre[i-1];
		out[j]+=a[i];
	}
	ll k=0;
	for(ll i=1;i<=n;i++)
	{
		k+=m[i];

		out[i]+=(k*b[i]);
		cout<<out[i]<<" ";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
	// your code goes here
	ll n;
	cin>>n;
	ll a[n];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	ll b[n+1];
	ll pre[n+1];
	pre[0]=0;
	for(ll j=1;j<=n;j++)
	{
		cin>>b[j];
		pre[j]=b[j];
		if(j)
		{
			pre[j]+=pre[j-1];
		}
	}
	multiset<pair<ll,ll>> s;
	ll k=0;
	for(ll i=0;i<n;i++)
	{
		s.insert(make_pair(i,a[i]));
		ll out=0;
		while(s.size())
		{
			pair<ll,ll> p=(*(s.begin()));
			if(pre[i+1]-pre[p.first]>=p.second)
			{
				out+=p.second-pre[i]+pre[p.first];
				s.erase(s.begin());
			}
			else
			{
				break;
			}
		}

		out+=s.size()*b[i+1];
		cout<<out<<" ";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll x,cnt=0,a[0],y,cnt2,sum;
    cin>>x;
    while(cnt!=x)
    {
      std::cin >> a[cnt];
      cnt++;
    }
    cnt=0;
    while (cnt!=x)
    {
      cnt2=0;
      sum=0;
      cin>>y;
      while (cnt2<=cnt)
      {
        if(a[cnt2]<=y){sum += a[cnt2];a[cnt2]=0;}
        else{sum+=y;a[cnt2] -= y;}
        cnt2++;
      }
      cout<<sum<<"\n";
      cnt++;
    }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAXN = 1e6;
ll parsum[MAXN] , V[MAXN] , T[MAXN] , U[MAXN] , EZFE[MAXN];

int main()
{
  ll n;
  cin >> n;
  for(ll i = 1;i <= n;i++)
  {cin>>V[i];}

  for(ll i = 1;i <= n;i++)
  {
    cin>>T[i];
    parsum[i] = parsum[i-1] + T[i];
  }
  for(ll i = 1;i <= n;i++)
  {
    ll up = upper_bound(parsum,parsum+n+1,V[i]+parsum[i-1])-parsum  -    1;//f. this 1
    //cout<<up<<endl;
    U[i]++;U[up+1]--;
    ll y = parsum[up] - parsum[i-1];
    EZFE[up+1] += V[i]-y;
    U[i]+=U[i-1];
  }
  for(int i = 1 ;i <= n; i++)
  {
    cout << (U[i]*T[i])+EZFE[i]<<" ";
    //cout<<endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll x,cnt=0,y,cnt2,sum;
    cin>>x;
    ll a[x-1];
    while(cnt!=x)
    {
      cin >> a[cnt];
      cnt++;
    }
    cnt=0;
    while (cnt!=x)
    {
      cnt2=0;
      sum=0;
      cin>>y;
      while (cnt2<=cnt)
      {
        if(a[cnt2]<=y){sum += a[cnt2];a[cnt2]=0;}
        else{sum+=y;a[cnt2] -= y;}
        cnt2++;
      }
      cout<<sum<<"\n";
      cnt++;
    }

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

int main() {
	int n; cin >> n;
	long long volumes[n];
	long long temperatures[n + 1];
	temperatures[n] = 0;
	long long cumulative[n + 1];
	cumulative[n + 1] = 9999999999999;
	for(int i = 0; i < n; ++i) {
		cin >> volumes[i];
	}
	for(int i = 0; i < n; ++i) {
		cin >> temperatures[i];
		cumulative[i + 1] = temperatures[i];
	}
	cumulative[0] = 0;
	for(int i = 1; i < n + 2; ++i) {
		cumulative[i] += cumulative[i - 1];
	}
	vector<int> toremove[n + 2];
	long long melted[n + 2];
	for(int i = 0; i < n + 2; ++i) melted[i] = 0;
	long long total = 0;
	for(int i = 0; i < n; ++i) {
		long long curr = volumes[i];
		//cout << "curr + cum " << curr + cumulative[i] << "\n";
		int expires = upper_bound(cumulative, cumulative + n + 2, curr + cumulative[i]) - cumulative;
		//cout << expires << "\n";
		melted[expires - 1] += curr + cumulative[i] - cumulative[expires - 1];
		toremove[expires - 1].push_back(i);
		total += 1;
		for(int i : toremove[i]) {
			total -= 1;
		}
		melted[i] += total * temperatures[i];
		cout << melted[i] << " ";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

int main() {
	int n; cin >> n;
	long volumes[n];
	long temperatures[n + 1];
	temperatures[n] = 0;
	long cumulative[n + 1];
	cumulative[n + 1] = 9999999999999;
	for(int i = 0; i < n; ++i) {
		cin >> volumes[i];
	}
	for(int i = 0; i < n; ++i) {
		cin >> temperatures[i];
		cumulative[i + 1] = temperatures[i];
	}
	cumulative[0] = 0;
	for(int i = 1; i < n + 2; ++i) {
		cumulative[i] += cumulative[i - 1];
	}
	vector<int> toremove[n + 1];
	long melted[n];
	for(int i = 0; i < n; ++i) melted[i] = 0;
	long total = 0;
	for(int i = 0; i < n; ++i) {
		int curr = volumes[i];
		//cout << "curr + cum " << curr + cumulative[i] << "\n";
		int expires = upper_bound(cumulative, cumulative + n + 2, curr + cumulative[i]) - cumulative;
		//cout << expires << "\n";
		melted[expires - 1] += curr + cumulative[i] - cumulative[expires - 1];
		toremove[expires - 1].push_back(i);
		total += 1;
		for(int i : toremove[i]) {
			total -= 1;
		}
		melted[i] += total * temperatures[i];
		cout << melted[i] << " ";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

int main() {
	int n; cin >> n;
	long volumes[n];
	long temperatures[n + 1];
	temperatures[n] = 0;
	long cumulative[n + 1];
	cumulative[n + 1] = 9999999999999;
	for(int i = 0; i < n; ++i) {
		cin >> volumes[i];
	}
	for(int i = 0; i < n; ++i) {
		cin >> temperatures[i];
		cumulative[i + 1] = temperatures[i];
	}
	cumulative[0] = 0;
	for(int i = 1; i < n + 2; ++i) {
		cumulative[i] += cumulative[i - 1];
	}
	vector<int> toremove[n + 2];
	long melted[n + 2];
	for(int i = 0; i < n; ++i) melted[i] = 0;
	long total = 0;
	for(int i = 0; i < n; ++i) {
		int curr = volumes[i];
		//cout << "curr + cum " << curr + cumulative[i] << "\n";
		int expires = upper_bound(cumulative, cumulative + n + 2, curr + cumulative[i]) - cumulative;
		//cout << expires << "\n";
		melted[expires - 1] += curr + cumulative[i] - cumulative[expires - 1];
		toremove[expires - 1].push_back(i);
		total += 1;
		for(int i : toremove[i]) {
			total -= 1;
		}
		melted[i] += total * temperatures[i];
		cout << melted[i] << " ";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

int main() {
	int n; cin >> n;
	long volumes[n];
	long temperatures[n + 1];
	temperatures[n] = 0;
	long cumulative[n + 1];
	cumulative[n + 1] = 9999999999999;
	for(int i = 0; i < n; ++i) {
		cin >> volumes[i];
	}
	for(int i = 0; i < n; ++i) {
		cin >> temperatures[i];
		cumulative[i + 1] = temperatures[i];
	}
	cumulative[0] = 0;
	for(int i = 1; i < n + 2; ++i) {
		cumulative[i] += cumulative[i - 1];
	}
	vector<int> toremove[n + 1];
	long melted[n + 1];
	for(int i = 0; i < n; ++i) melted[i] = 0;
	long total = 0;
	for(int i = 0; i < n; ++i) {
		int curr = volumes[i];
		//cout << "curr + cum " << curr + cumulative[i] << "\n";
		int expires = upper_bound(cumulative, cumulative + n + 2, curr + cumulative[i]) - cumulative;
		//cout << expires << "\n";
		melted[expires - 1] += curr + cumulative[i] - cumulative[expires - 1];
		toremove[expires - 1].push_back(i);
		total += 1;
		for(int i : toremove[i]) {
			total -= 1;
		}
		melted[i] += total * temperatures[i];
		cout << melted[i] << " ";
	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
using namespace std;
typedef long long li;
const li MAX = 10e5 + 1;
vector <li> l, temp, pref;
li n, m, les[MAX], cnt[MAX], amount[MAX];

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++) {
		li u;
		cin >> u;
		l.push_back(u);
	}
	for (int i = 0; i < n; i++) {
		li u;
		cin >> u;
		temp.push_back(u);
	}
	pref.push_back(temp[0]);
	for (int i = 1; i < n; i++) {
		pref.push_back(pref[i - 1] + temp[i]);
	}
	for (int i = 1; i < n; i++) {
		l[i] += pref[i - 1];
	}
	for (int i = 1; i <= n; i++) {
		amount[i] = amount[i - 1] + 1;
		li uk1 = -1, uk2 = n;
		while (uk2 - uk1 > 1) {
			if (pref[(uk2 + uk1) / 2] <= l[i - 1]) {
				uk1 = (uk2 + uk1) / 2;
			}
			else {
				uk2 = (uk2 + uk1) / 2;
			}
		}
		cnt[uk2] += 1;
		les[uk2] += l[i - 1] - pref[uk2 - 1];
		amount[i] -= cnt[i - 1];
	}
	for (int i = 0; i < n; i++) {
		cout << amount[i + 1] * temp[i] + les[i] << " ";
	}
//	system("pause");
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
using namespace std;
typedef long long li;
const li MAX = 10e5 + 1;
vector <li> l, temp, pref;
li n, m, les[MAX], cnt[MAX], amount[MAX], k = 0;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++) {
		li u;
		cin >> u;
		l.push_back(u);
	}
	for (int i = 0; i < n; i++) {
		li u;
		cin >> u;
		temp.push_back(u);
	}
	pref.push_back(temp[0]);
	for (int i = 1; i < n; i++) {
		pref.push_back(pref[i - 1] + temp[i]);
	}
	for (int i = 1; i < n; i++) {
		l[i] += pref[i - 1];
	}
	for (int i = 1; i <= n; i++) {
		amount[i] = amount[i - 1] + 1;
		li uk1 = -1, uk2 = n;
		while (uk2 - uk1 > 1) {
			if (pref[(uk2 + uk1) / 2] <= l[i - 1]) {
				uk1 = (uk2 + uk1) / 2;
			}
			else {
				uk2 = (uk2 + uk1) / 2;
			}
		}
		if (uk1 != n - 1) {
			cnt[uk1 + 1] += 1;
			les[uk1 + 1] += max(l[i - 1] - pref[uk1], k);
		}
		amount[i] -= cnt[i - 1];
	}

	for (int i = 0; i < n; i++) {
		cout << amount[i + 1] * temp[i] + les[i] << " ";
	}
	//system("pause");
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
using namespace std;
typedef long long li;
const li MAX = 10e5 + 1;
vector <li> l, temp, pref;
li n, m, les[MAX], cnt[MAX], amount[MAX], k = 0;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++) {
		li u;
		cin >> u;
		l.push_back(u);
	}
	for (int i = 0; i < n; i++) {
		li u;
		cin >> u;
		temp.push_back(u);
	}
	pref.push_back(temp[0]);
	for (int i = 1; i < n; i++) {
		pref.push_back(pref[i - 1] + temp[i]);
	}
	for (int i = 1; i < n; i++) {
		l[i] += pref[i - 1];
	}
	for (int i = 1; i <= n; i++) {
		amount[i] = amount[i - 1] + 1;
		li uk1 = -1, uk2 = n;
		while (uk2 - uk1 > 1) {
			if (pref[(uk2 + uk1) / 2] <= l[i - 1]) {
				uk1 = (uk2 + uk1) / 2;
			}
			else {
				uk2 = (uk2 + uk1) / 2;
			}
		}
		if (uk1 != n - 1) {
			cnt[uk1 + 1] += 1;
			if (uk1 != -1)
			les[uk1 + 1] += max(l[i - 1] - pref[uk1], k);
			else les[uk1 + 1] = l[i - 1];
		}
		amount[i] -= cnt[i - 1];
	}

	for (int i = 0; i < n; i++) {
		cout << amount[i + 1] * temp[i] + les[i] << " ";
	}
	//system("pause");
	return 0;
}


//  This code is written by Eric Sunli Chen ( OhWeOnFire ).
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <map>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
template<typename T>
void get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'; t=getchar());
	if(t=='-')neg=true,t=getchar();
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;
}
template<typename T>
void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) get1(a),get1(b)
#define get3(a,b,c) get1(a),get2(b,c)
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);
const int maxlog=20;
const int maxn=100005;
const int Alpha=26;

int n,k,ex[maxn],ey[maxn],num,ord[maxn],rnk[maxn];
vector<int> g[maxn];
bool use[maxn],chain[maxn];
void dfs(int x)
{
	use[x]=true;
	for(int i=0;i<(int)g[x].size();i++)
		if(!use[g[x][i]])
			dfs(g[x][i]);
	num++;
	ord[num]=x;
	rnk[x]=num;
}
int main()
{
	get2(n,k);
	for(int i=1;i<=k;i++)
	{
		get2(ex[i],ey[i]);
		g[ex[i]].pb(ey[i]);
	}
	for(int i=1;i<=n;i++)if(!use[i])dfs(i);
	int t=0;
	for(int i=1;i<=k;i++)
	{
		if(rnk[ey[i]]=rnk[ex[i]]-1)t++;
		if(t==n)
		{
			printendl(i);
			return 0;
		}
	}
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

map<int,set<int>> G;
int n,m;
vector<bool> visited;
vector<int> in_degree;
bool check_unique(){
    for(int i=0;i<n;i++) visited[i] =false;
    int curr = -1;
    for(int i=0;i<n;i++){
        if (in_degree[i]==0){
            curr = i;
            break;
        }
    }
    if (curr == -1) return false;
    visited[curr] = true;
    while(G[curr].size() > 0){
        curr = *G[curr].begin();
        //if (visited[curr]) return false;
        visited[curr] = true;
    }
    for(int i=0;i<visited.size();i++) if (!visited[i]) return false;
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int u,v;
    cin >> n >> m;
    visited.resize(n);
    in_degree.resize(n);
    for(auto&x : in_degree) x = 0;
    vector<pair<int,int>> vec(m);
    for(int i=0;i<m;i++){
        cin >> u >> v;
        u--;v--;
        vec[i] = {u,v};
    }
    int l = 0,r=m;
    int last = 0;
    int ans = -1;
    while(l<=r){
        int m = (r-l)/2 + l;
        if (m > last){
        for(int i=last;i<m;i++){
            int v=vec[i].first,u=vec[i].second;
            G[v].insert(u);
            in_degree[u]++;
        }
        }
        else if (m < last){
         for(int i=m;i<last;i++){
            int v=vec[i].first,u=vec[i].second;
            G[v].erase(G[v].find(u));
            in_degree[u]--;
        }
        }
         last = m;
         if (check_unique()){
            ans = m;
            r = m-1;
         }
         else l = m+1;

    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> G;
int n,m;
vector<bool> visited;
vector<int> in_degree;
bool check_unique(){
    for(int i=0;i<n;i++) visited[i] =false;
    int curr = -1;
    queue<int> q;
    for(int i=0;i<n;i++){
        if (in_degree[i]==0){
            q.push(i);
        }
    }
    int l = 0;
    while(!q.empty()){
        if(q.size() > 1) return false;
        int u = q.front();
        q.pop();
        l++;
        for(auto&v : G[u]){
            in_degree[v]--;
            if(in_degree[v]==0) q.push(v);
        }
    }
    return l==n;
}

void build_g(vector<pair<int,int>>& vec,int m){
    for(auto&x : G) x.clear();
    for(auto&x : in_degree) x = 0;
    for(int i=0;i<m;i++){
        int u=vec[i].first,v=vec[i].second;
        G[u].push_back(v);
        in_degree[v]++;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int u,v;
    cin >> n >> m;
    visited.resize(n);
    in_degree.resize(n);
    for(auto&x : in_degree) x = 0;
    vector<pair<int,int>> vec(m);
    for(int i=0;i<m;i++){
        cin >> u >> v;
        u--;v--;
        vec[i] = {u,v};
    }
    G.resize(n);
    int l = 0,r=m;
    int last = 0;
    int ans = -1;
    while(l<=r){
        int m = (r-l)/2 + l;
        build_g(vec,m);
         if (check_unique()){
            ans = m;
            r = m-1;
         }
         else l = m+1;

    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

map<int,set<int>> G;
int n,m;
vector<bool> visited;
vector<int> in_degree;
bool check_unique(){
    for(int i=0;i<n;i++) visited[i] =false;
    int curr = -1;
    for(int i=0;i<n;i++){
        if (in_degree[i]==0){
            curr = i;
            break;
        }
    }
    if (curr == -1) return false;
    visited[curr] = true;
    while(G[curr].size() > 0){
        curr = *G[curr].begin();
        if (visited[curr]) return false;
        visited[curr] = true;
    }
    for(int i=0;i<visited.size();i++) if (!visited[i]) return false;
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int u,v;
    cin >> n >> m;
    visited.resize(n);
    in_degree.resize(n);
    for(auto&x : in_degree) x = 0;
    vector<pair<int,int>> vec(m);
    for(int i=0;i<m;i++){
        cin >> u >> v;
        u--;v--;
        vec[i] = {u,v};
    }
    int l = 1,r=m;
    int last = 0;
    int ans = -1;
    while(l<=r){
        int m = (r-l)/2 + l;
        if (m > last){
        for(int i=last;i<m;i++){
            int u=vec[i].first,v=vec[i].second;
            G[v].insert(u);
            in_degree[u]++;
        }
        }
        else if (m < last){
         for(int i=m;i<last;i++){
            int u=vec[i].first,v=vec[i].second;
            G[v].erase(G[v].find(u));
            in_degree[u]--;
        }
        }
         last = m;
         if (check_unique()){
            ans = m;
            r = m-1;
         }
         else l = m+1;

    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

map<int,set<int>> G;
int n,m;
vector<bool> visited;
vector<int> in_degree;
bool check_unique(){
    for(int i=0;i<n;i++) visited[i] =false;
    int curr = -1;
    for(int i=0;i<n;i++){
        if (in_degree[i]==0){
            curr = i;
            break;
        }
    }
    if (curr == -1) return false;
    visited[curr] = true;
    while(G[curr].size() > 0){
        curr = *G[curr].begin();
        //if (visited[curr]) return false;
        visited[curr] = true;
    }
    for(int i=0;i<visited.size();i++) if (!visited[i]) return false;
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int u,v;
    cin >> n >> m;
    visited.resize(n);
    in_degree.resize(n);
    for(auto&x : in_degree) x = 0;
    vector<pair<int,int>> vec(m);
    for(int i=0;i<m;i++){
        cin >> u >> v;
        u--;v--;
        vec[i] = {u,v};
    }
    int l = 1,r=m;
    int last = 0;
    int ans = -1;
    while(l<=r){
        int m = (r-l)/2 + l;
        if (m > last){
        for(int i=last;i<m;i++){
            int u=vec[i].first,v=vec[i].second;
            G[v].insert(u);
            in_degree[u]++;
        }
        }
        else if (m < last){
         for(int i=m;i<last;i++){
            int u=vec[i].first,v=vec[i].second;
            G[v].erase(G[v].find(u));
            in_degree[u]--;
        }
        }
         last = m;
         if (check_unique()){
            ans = m;
            r = m-1;
         }
         else l = m+1;

    }
    cout << ans << '\n';
    return 0;
}


#include<bits/stdc++.h>
#include <iostream>
#include <numeric>
#define ll  long long
#define ull unsigned long long
#define mpa make_pair
#define pb push_back
#define ff first
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n)    for(ll i=0;i<n;++i)
#define ren(i,n)    for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define fre     freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) printf("%lld\n",x)
#define gc getchar
using namespace std;
template<class T> inline void umax(T &a,T b) {
	if(a<b) a = b ;
}
template<class T> inline void umin(T &a,T b) {
	if(a>b) a = b ;
}
template<class T> inline T abs(T a) {
	return a>0 ? a : -a;
}
template<class T> inline T gcd(T a,T b) {
	return __gcd(a, b);
}
template<class T> inline T lcm(T a,T b) {
	return a/gcd(a,b)*b;
}
ll modpow(ll a,ll n,ll temp) {
	ll res=1,y=a;
	while(n>0) {
		if(n&1)res=(res*y)%temp;
		y=(y*y)%temp;
		n/=2;
	}
	return res%temp;
}
//ll a[100005],n,dp[1000005]={0},b[1000005],c[100005],dp1[1000005],bit[100005];
//ll vis[100005];
//vector<ll> adj[100005];

#define sz(x) x.size()
ll ison(ll mask,ll pos) {
	return (mask&(1<<pos));
}
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
ll cbit(ll n) {
	ll k=0;
	while(n) n&=(n-1),k++;
	return k;
}
ll nbit(ll n) {
	ll k=0;
	while(n) n/=2,k++;
	return k;
}
ll mod=1e9+7;
int sgn(ll x) {
	return x < 0 ? -1 : !!x;
}
const double eps = 1e-7;
const double pi = acos(-1.0);
inline void inc(ll &x, ll y){
	x+=y;
	if(x>=mod)x-=mod;
}
inline void dec(ll &x, ll y){
	x-=y;
	if(x<0) x+=mod;
}
inline void chmax(ll &x,ll y){
	if(y>x) x=y;
}

inline void mulm(ll &x,ll y){
	x*=y;
	if(x>=mod) x-=mod;
}

//pair<ll,ll> a[1000005];
/*
void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}*/

//ll tree[500005];

//vector<ll,pair<ll,ll> > b;
//vector<ll> adj[100005];

/*Tarjan incomplette
void dfs(ll s){
	vis[s]=1;
	for(ll i=0;i<adj[s].size();i++) if(!vis[adj[s][i]]) dfs(adj[s][i]);
    ofvs.pb(s);
}
void dfs2(ll s){
	vis[s]=1;
	if(wt[s]<optc){optn=0,optc=wt[s];}
	if(optc==wt[s]) optn++;
	for(ll i=0;i<rdj[s].size();i++) if(!vis[rdj[s][i]]) dfs2(rdj[s][i]);
}
*/
typedef pair<ll,pair<ll,ll> >ver;
//string s;
/*ll tree[200005];
ll a[100005];
void build(ll node,ll ss,ll se)
{
	if(ss==se) tree[node]=a[ss];
	else
	{
		ll mid=(ss+se)/2;
		build(2*node,ss,mid);
		build(2*node+1,mid+1,se);
        tree[node]=tree[2*node]&tree[2*node+1];
	}
}*/
/*void upd(ll node,ll ss,ll se,ll idx,ll val)
{
	if(ss==se)
	a[idx]=val,tree[node]=a[idx];
	else
	{
		ll mid=(ss+se)/2;
		if(ss<=idx && idx<=mid)
		upd(2*node,ss,mid,idx,val);
		else
		upd(2*node+1,mid+1,se,idx,val);
	    tree[node]=tree[node*2]+tree[node*2+1];
	}
}*/
/*ll query(ll node,ll ss,ll se,ll l,ll r)
{
	if(r<ss || se<l)
	return 0;
	if(l<=ss && se<=r)
	return tree[node];
	ll mid=(ss+se)/2;
	ll q1=query(node*2,ss,mid,l,r);
	ll q2=query(node*2+1,mid+1,se,l,r);
	return q1&q2;
}*/
/*ll pow(ll n){
	ll t=1,i=1;
	while(i<=n) t*=10;
	return t;
}*/
//stack<ll> help;
//ll dp[1ll<<11];

//ll csum[100005],b[100005],help[100005];
//vector<ll> adj[1005];
//vector<ll> ans;
/*ll kmp[1000005];
ll zee[1000005];
void kmpm(string p){
	ll i,j,k=0,l;
	kmp[0]=0;
	j=0;
	for(i=1;i<p.size();i++){
		while(k && p[i]!=p[k]) k=kmp[k-1];
		if(p[i]==p[k]) k++;
		kmp[i]=k;
	}
}
ll isp(ll n){
	ll f=1,i;
	for(i=2;i<=sqrt(n);i++) if(!(n%i)) return 0;
	return 1;
}*/
/*vector<ll> zFunction(vector<ll> &s) {
    int n = (ll) s.size();
    vector<ll> z (n);
    for (int i=1, l=0, r=0; i<n; ++i) {
        if (i <= r)
            z[i] = min (r-i+1, z[i-l]);
        while (i+z[i] < n && s[z[i]] == s[i+z[i]])
            ++z[i];
        if (i+z[i]-1 > r)
            l = i,  r = i+z[i]-1;
    }
    return z;
}*/
//map<ll,ll> help;
/*ll chk(ll val,ll n,ll m){
	ll t=0,i;
	val--;
	for(i=1;i<=n;i++) t+=min(m,val/i);
	return t;
}*/

/*ll size[100005];
void init(ll n)
{for(int i=1;i<=n;i++) a[i]=i;}
ll root(ll x)
{
	while(a[x]!=x)
	{
		a[x]=a[a[x]];
		x=a[x];
	}
	return x;
}
ll u1(ll x,ll y)
{
	int p=root(x);
	int q=root(y);
	a[p]=a[q];
}
*/
/*ll tree[100005];
void update(ll idx, ll val){
	while(idx < 100005){
		tree[idx] = max(tree[idx], val);
		idx += (idx & (-idx));
	}
}
ll query(ll idx){
	ll ret = 0;
	while(idx > 0){
		ret = max(ret, tree[idx]);
		idx -= (idx & (-idx));
	}
	return ret;
}*/
//pair<char ,pair<ll,ll> >p[5005];[10004]
/*
//(topsort)
vector<ll> adj[100005];
vector<ll> fino;
ll vis[100005];
void dfs(ll v){
vis[v]=1;
for(ll i=0;i<adj[v].size();i++) if(!vis[adj[v][i]])dfs(adj[v][i]);
fino.pb(v);
}
*/

ll xo(ll i) {
	if ((i&3)==0) return i;
	if ((i&3)==1) return 1;
	if ((i&3)==2) return i+1;
	return 0;
}
//pair<ll,ll> p[10005];
/*ll a[100005];
ll bit[100005];
ll lmr[1000005];
ll pi[1000005];
ll ans[1000005];
void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}
pair<pair<ll,ll>,ll> >query[1000005];
*/

//pair<ll,ll> a[1000005];
/*
void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}*/

//ll tree[500005];

//vector<ll,pair<ll,ll> > b;
//vector<ll> adj[100005];

/*Tarjan incomplette
void dfs(ll s){
	vis[s]=1;
	for(ll i=0;i<adj[s].size();i++) if(!vis[adj[s][i]]) dfs(adj[s][i]);
    ofvs.pb(s);
}
void dfs2(ll s){
	vis[s]=1;
	if(wt[s]<optc){optn=0,optc=wt[s];}
	if(optc==wt[s]) optn++;
	for(ll i=0;i<rdj[s].size();i++) if(!vis[rdj[s][i]]) dfs2(rdj[s][i]);
}
*/
typedef pair<ll,pair<ll,ll> >ver;
//string s;
/*ll tree[200005];
ll a[100005];
void build(ll node,ll ss,ll se)
{
	if(ss==se) tree[node]=a[ss];
	else
	{
		ll mid=(ss+se)/2;
		build(2*node,ss,mid);
		build(2*node+1,mid+1,se);
        tree[node]=tree[2*node]&tree[2*node+1];
	}
}*/
/*void upd(ll node,ll ss,ll se,ll idx,ll val)
{
	if(ss==se)
	a[idx]=val,tree[node]=a[idx];
	else
	{
		ll mid=(ss+se)/2;
		if(ss<=idx && idx<=mid)
		upd(2*node,ss,mid,idx,val);
		else
		upd(2*node+1,mid+1,se,idx,val);
	    tree[node]=tree[node*2]+tree[node*2+1];
	}
}*/
/*ll query(ll node,ll ss,ll se,ll l,ll r)
{
	if(r<ss || se<l)
	return 0;
	if(l<=ss && se<=r)
	return tree[node];
	ll mid=(ss+se)/2;
	ll q1=query(node*2,ss,mid,l,r);
	ll q2=query(node*2+1,mid+1,se,l,r);
	return q1&q2;
}*/
/*ll pow(ll n){
	ll t=1,i=1;
	while(i<=n) t*=10;
	return t;
}*/
//stack<ll> help;
//ll dp[1ll<<11];

//ll csum[100005],b[100005],help[100005];
//vector<ll> adj[1005];
//vector<ll> ans;
/*ll kmp[1000005];
ll zee[1000005];
void kmpm(string p){
	ll i,j,k=0,l;
	kmp[0]=0;
	j=0;
	for(i=1;i<p.size();i++){
		while(k && p[i]!=p[k]) k=kmp[k-1];
		if(p[i]==p[k]) k++;
		kmp[i]=k;
	}
}
ll isp(ll n){
	ll f=1,i;
	for(i=2;i<=sqrt(n);i++) if(!(n%i)) return 0;
	return 1;
}*/
/*vector<ll> zFunction(vector<ll> &s) {
    int n = (ll) s.size();
    vector<ll> z (n);
    for (int i=1, l=0, r=0; i<n; ++i) {
        if (i <= r)
            z[i] = min (r-i+1, z[i-l]);
        while (i+z[i] < n && s[z[i]] == s[i+z[i]])
            ++z[i];
        if (i+z[i]-1 > r)
            l = i,  r = i+z[i]-1;
    }
    return z;
}*/
//map<ll,ll> help;
/*ll chk(ll val,ll n,ll m){
	ll t=0,i;
	val--;
	for(i=1;i<=n;i++) t+=min(m,val/i);
	return t;
}*/

/*ll size[100005];
void init(ll n)
{for(int i=1;i<=n;i++) a[i]=i;}
ll root(ll x)
{
	while(a[x]!=x)
	{
		a[x]=a[a[x]];
		x=a[x];
	}
	return x;
}
ll u1(ll x,ll y)
{
	int p=root(x);
	int q=root(y);
	a[p]=a[q];
}
*/
/*ll tree[100005];
void update(ll idx, ll val){
	while(idx < 100005){
		tree[idx] = max(tree[idx], val);
		idx += (idx & (-idx));
	}
}
ll query(ll idx){
	ll ret = 0;
	while(idx > 0){
		ret = max(ret, tree[idx]);
		idx -= (idx & (-idx));
	}
	return ret;
}*/
//pair<char ,pair<ll,ll> >p[5005];[10004]
/*
//(topsort)
vector<ll> adj[100005];
vector<ll> fino;
ll vis[100005];
void dfs(ll v){
vis[v]=1;
for(ll i=0;i<adj[v].size();i++) if(!vis[adj[v][i]])dfs(adj[v][i]);
fino.pb(v);
}
*/
/*ll anc[20][100005];
ll d[100005];
vector<ll> adj[100005];
void dfs(ll v,ll p){
    for(ll i=0;i<adj[v].size();i++){
        if(adj[v][i]!=p){
            d[adj[v][i]]=d[v]+1;
            anc[0][adj[v][i]]=v;
            dfs(adj[v][i],v);
        }
    }
}

ll lca(ll a,ll b){
    if(d[a]>d[b])swap(a,b);
    ll diff=d[b]-d[a];
    for(ll i=0;i<20;i++)if(diff&(1<<i))b=anc[i][b];
    for(ll i=19;i>=0;i--){
        if(anc[i][a]!=anc[i][b]) a=anc[i][a],b=anc[i][b];
    }
    if(a==b)return a;
    return anc[0][a];
}
ll dist(ll a,ll b){
    ll l=lca(a,b);
    return d[a]+d[b]-2*d[l];
}*/
//pair<ll,ll> p[10005];
/*ll a[100005];
ll bit[100005];
ll lmr[1000005];
ll pi[1000005];
ll ans[1000005];
void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}
pair<pair<ll,ll>,ll> >query[1000005];
*/

//pair<ll,ll> a[205];
//template for #trie
//vector<ll> adj[100005],adjr[100005];

//ll a[100005],dp[100005];
pair<ll,ll> edges[100005];

vector<ll> adj[100005];
ll n;

ll check(ll mid){

	ll i,j,k,l,s;
	ll indg[100005]={0},pr[100005]={0};
    for(i=1;i<=n;i++) adj[i].clear();
    for(i=1;i<=mid;i++) adj[edges[i].ff].pb(edges[i].ss),pr[edges[i].ff]=1,pr[edges[i].ss]=1,indg[edges[i].ss]++;
    queue<ll> q;
    j=0;
    for(i=1;i<=n;i++) if(pr[i] && indg[i]==0) q.push(i);
    while(q.size()){
    	if(q.size()>1) return 0;
		ll v=q.front();
		j++;
		q.pop();
		for(ll i=0;i<adj[v].size();i++) {
			indg[adj[v][i]]--;
			if(indg[adj[v][i]]==0) q.push(adj[v][i]);
		}
    }
    return (j==n);
}
int main(){
	ll q,i,j,k,l,m;
    cin>>n>>m;
    for(i=1;i<=m;i++) cin>>edges[i].ff>>edges[i].ss;
    ll hi=m;
    ll lo=1;
    ll ans=mod;
    while(hi>=lo){
    	ll mid=(hi+lo)/2;
    	if(check(mid)) ans=min(ans,mid),hi=mid-1;
    	else lo=mid+1;
    }
    if(ans==mod)cout<<-1;
    else cout<<ans;
}


#include<bits/stdc++.h>
#include <iostream>
#include <numeric>
#define ll  long long
#define ull unsigned long long
#define mpa make_pair
#define pb push_back
#define ff first
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n)    for(ll i=0;i<n;++i)
#define ren(i,n)    for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define fre     freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) printf("%lld\n",x)
#define gc getchar
using namespace std;
template<class T> inline void umax(T &a,T b) {
	if(a<b) a = b ;
}
template<class T> inline void umin(T &a,T b) {
	if(a>b) a = b ;
}
template<class T> inline T abs(T a) {
	return a>0 ? a : -a;
}
template<class T> inline T gcd(T a,T b) {
	return __gcd(a, b);
}
template<class T> inline T lcm(T a,T b) {
	return a/gcd(a,b)*b;
}
ll modpow(ll a,ll n,ll temp) {
	ll res=1,y=a;
	while(n>0) {
		if(n&1)res=(res*y)%temp;
		y=(y*y)%temp;
		n/=2;
	}
	return res%temp;
}
//ll a[100005],n,dp[1000005]={0},b[1000005],c[100005],dp1[1000005],bit[100005];
//ll vis[100005];
//vector<ll> adj[100005];

#define sz(x) x.size()
ll ison(ll mask,ll pos) {
	return (mask&(1<<pos));
}
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
ll cbit(ll n) {
	ll k=0;
	while(n) n&=(n-1),k++;
	return k;
}
ll nbit(ll n) {
	ll k=0;
	while(n) n/=2,k++;
	return k;
}
ll mod=1e9+7;
int sgn(ll x) {
	return x < 0 ? -1 : !!x;
}
const double eps = 1e-7;
const double pi = acos(-1.0);
inline void inc(ll &x, ll y){
	x+=y;
	if(x>=mod)x-=mod;
}
inline void dec(ll &x, ll y){
	x-=y;
	if(x<0) x+=mod;
}
inline void chmax(ll &x,ll y){
	if(y>x) x=y;
}

inline void mulm(ll &x,ll y){
	x*=y;
	if(x>=mod) x-=mod;
}

//pair<ll,ll> a[1000005];
/*
void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}*/

//ll tree[500005];

//vector<ll,pair<ll,ll> > b;
//vector<ll> adj[100005];

/*Tarjan incomplette
void dfs(ll s){
	vis[s]=1;
	for(ll i=0;i<adj[s].size();i++) if(!vis[adj[s][i]]) dfs(adj[s][i]);
    ofvs.pb(s);
}
void dfs2(ll s){
	vis[s]=1;
	if(wt[s]<optc){optn=0,optc=wt[s];}
	if(optc==wt[s]) optn++;
	for(ll i=0;i<rdj[s].size();i++) if(!vis[rdj[s][i]]) dfs2(rdj[s][i]);
}
*/
typedef pair<ll,pair<ll,ll> >ver;
//string s;
/*ll tree[200005];
ll a[100005];
void build(ll node,ll ss,ll se)
{
	if(ss==se) tree[node]=a[ss];
	else
	{
		ll mid=(ss+se)/2;
		build(2*node,ss,mid);
		build(2*node+1,mid+1,se);
        tree[node]=tree[2*node]&tree[2*node+1];
	}
}*/
/*void upd(ll node,ll ss,ll se,ll idx,ll val)
{
	if(ss==se)
	a[idx]=val,tree[node]=a[idx];
	else
	{
		ll mid=(ss+se)/2;
		if(ss<=idx && idx<=mid)
		upd(2*node,ss,mid,idx,val);
		else
		upd(2*node+1,mid+1,se,idx,val);
	    tree[node]=tree[node*2]+tree[node*2+1];
	}
}*/
/*ll query(ll node,ll ss,ll se,ll l,ll r)
{
	if(r<ss || se<l)
	return 0;
	if(l<=ss && se<=r)
	return tree[node];
	ll mid=(ss+se)/2;
	ll q1=query(node*2,ss,mid,l,r);
	ll q2=query(node*2+1,mid+1,se,l,r);
	return q1&q2;
}*/
/*ll pow(ll n){
	ll t=1,i=1;
	while(i<=n) t*=10;
	return t;
}*/
//stack<ll> help;
//ll dp[1ll<<11];

//ll csum[100005],b[100005],help[100005];
//vector<ll> adj[1005];
//vector<ll> ans;
/*ll kmp[1000005];
ll zee[1000005];
void kmpm(string p){
	ll i,j,k=0,l;
	kmp[0]=0;
	j=0;
	for(i=1;i<p.size();i++){
		while(k && p[i]!=p[k]) k=kmp[k-1];
		if(p[i]==p[k]) k++;
		kmp[i]=k;
	}
}
ll isp(ll n){
	ll f=1,i;
	for(i=2;i<=sqrt(n);i++) if(!(n%i)) return 0;
	return 1;
}*/
/*vector<ll> zFunction(vector<ll> &s) {
    int n = (ll) s.size();
    vector<ll> z (n);
    for (int i=1, l=0, r=0; i<n; ++i) {
        if (i <= r)
            z[i] = min (r-i+1, z[i-l]);
        while (i+z[i] < n && s[z[i]] == s[i+z[i]])
            ++z[i];
        if (i+z[i]-1 > r)
            l = i,  r = i+z[i]-1;
    }
    return z;
}*/
//map<ll,ll> help;
/*ll chk(ll val,ll n,ll m){
	ll t=0,i;
	val--;
	for(i=1;i<=n;i++) t+=min(m,val/i);
	return t;
}*/

/*ll size[100005];
void init(ll n)
{for(int i=1;i<=n;i++) a[i]=i;}
ll root(ll x)
{
	while(a[x]!=x)
	{
		a[x]=a[a[x]];
		x=a[x];
	}
	return x;
}
ll u1(ll x,ll y)
{
	int p=root(x);
	int q=root(y);
	a[p]=a[q];
}
*/
/*ll tree[100005];
void update(ll idx, ll val){
	while(idx < 100005){
		tree[idx] = max(tree[idx], val);
		idx += (idx & (-idx));
	}
}
ll query(ll idx){
	ll ret = 0;
	while(idx > 0){
		ret = max(ret, tree[idx]);
		idx -= (idx & (-idx));
	}
	return ret;
}*/
//pair<char ,pair<ll,ll> >p[5005];[10004]
/*
//(topsort)
vector<ll> adj[100005];
vector<ll> fino;
ll vis[100005];
void dfs(ll v){
vis[v]=1;
for(ll i=0;i<adj[v].size();i++) if(!vis[adj[v][i]])dfs(adj[v][i]);
fino.pb(v);
}
*/

ll xo(ll i) {
	if ((i&3)==0) return i;
	if ((i&3)==1) return 1;
	if ((i&3)==2) return i+1;
	return 0;
}
//pair<ll,ll> p[10005];
/*ll a[100005];
ll bit[100005];
ll lmr[1000005];
ll pi[1000005];
ll ans[1000005];
void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}
pair<pair<ll,ll>,ll> >query[1000005];
*/

//pair<ll,ll> a[1000005];
/*
void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}*/

//ll tree[500005];

//vector<ll,pair<ll,ll> > b;
//vector<ll> adj[100005];

/*Tarjan incomplette
void dfs(ll s){
	vis[s]=1;
	for(ll i=0;i<adj[s].size();i++) if(!vis[adj[s][i]]) dfs(adj[s][i]);
    ofvs.pb(s);
}
void dfs2(ll s){
	vis[s]=1;
	if(wt[s]<optc){optn=0,optc=wt[s];}
	if(optc==wt[s]) optn++;
	for(ll i=0;i<rdj[s].size();i++) if(!vis[rdj[s][i]]) dfs2(rdj[s][i]);
}
*/
typedef pair<ll,pair<ll,ll> >ver;
//string s;
/*ll tree[200005];
ll a[100005];
void build(ll node,ll ss,ll se)
{
	if(ss==se) tree[node]=a[ss];
	else
	{
		ll mid=(ss+se)/2;
		build(2*node,ss,mid);
		build(2*node+1,mid+1,se);
        tree[node]=tree[2*node]&tree[2*node+1];
	}
}*/
/*void upd(ll node,ll ss,ll se,ll idx,ll val)
{
	if(ss==se)
	a[idx]=val,tree[node]=a[idx];
	else
	{
		ll mid=(ss+se)/2;
		if(ss<=idx && idx<=mid)
		upd(2*node,ss,mid,idx,val);
		else
		upd(2*node+1,mid+1,se,idx,val);
	    tree[node]=tree[node*2]+tree[node*2+1];
	}
}*/
/*ll query(ll node,ll ss,ll se,ll l,ll r)
{
	if(r<ss || se<l)
	return 0;
	if(l<=ss && se<=r)
	return tree[node];
	ll mid=(ss+se)/2;
	ll q1=query(node*2,ss,mid,l,r);
	ll q2=query(node*2+1,mid+1,se,l,r);
	return q1&q2;
}*/
/*ll pow(ll n){
	ll t=1,i=1;
	while(i<=n) t*=10;
	return t;
}*/
//stack<ll> help;
//ll dp[1ll<<11];

//ll csum[100005],b[100005],help[100005];
//vector<ll> adj[1005];
//vector<ll> ans;
/*ll kmp[1000005];
ll zee[1000005];
void kmpm(string p){
	ll i,j,k=0,l;
	kmp[0]=0;
	j=0;
	for(i=1;i<p.size();i++){
		while(k && p[i]!=p[k]) k=kmp[k-1];
		if(p[i]==p[k]) k++;
		kmp[i]=k;
	}
}
ll isp(ll n){
	ll f=1,i;
	for(i=2;i<=sqrt(n);i++) if(!(n%i)) return 0;
	return 1;
}*/
/*vector<ll> zFunction(vector<ll> &s) {
    int n = (ll) s.size();
    vector<ll> z (n);
    for (int i=1, l=0, r=0; i<n; ++i) {
        if (i <= r)
            z[i] = min (r-i+1, z[i-l]);
        while (i+z[i] < n && s[z[i]] == s[i+z[i]])
            ++z[i];
        if (i+z[i]-1 > r)
            l = i,  r = i+z[i]-1;
    }
    return z;
}*/
//map<ll,ll> help;
/*ll chk(ll val,ll n,ll m){
	ll t=0,i;
	val--;
	for(i=1;i<=n;i++) t+=min(m,val/i);
	return t;
}*/

/*ll size[100005];
void init(ll n)
{for(int i=1;i<=n;i++) a[i]=i;}
ll root(ll x)
{
	while(a[x]!=x)
	{
		a[x]=a[a[x]];
		x=a[x];
	}
	return x;
}
ll u1(ll x,ll y)
{
	int p=root(x);
	int q=root(y);
	a[p]=a[q];
}
*/
/*ll tree[100005];
void update(ll idx, ll val){
	while(idx < 100005){
		tree[idx] = max(tree[idx], val);
		idx += (idx & (-idx));
	}
}
ll query(ll idx){
	ll ret = 0;
	while(idx > 0){
		ret = max(ret, tree[idx]);
		idx -= (idx & (-idx));
	}
	return ret;
}*/
//pair<char ,pair<ll,ll> >p[5005];[10004]
/*
//(topsort)
vector<ll> adj[100005];
vector<ll> fino;
ll vis[100005];
void dfs(ll v){
vis[v]=1;
for(ll i=0;i<adj[v].size();i++) if(!vis[adj[v][i]])dfs(adj[v][i]);
fino.pb(v);
}
*/
/*ll anc[20][100005];
ll d[100005];
vector<ll> adj[100005];
void dfs(ll v,ll p){
    for(ll i=0;i<adj[v].size();i++){
        if(adj[v][i]!=p){
            d[adj[v][i]]=d[v]+1;
            anc[0][adj[v][i]]=v;
            dfs(adj[v][i],v);
        }
    }
}

ll lca(ll a,ll b){
    if(d[a]>d[b])swap(a,b);
    ll diff=d[b]-d[a];
    for(ll i=0;i<20;i++)if(diff&(1<<i))b=anc[i][b];
    for(ll i=19;i>=0;i--){
        if(anc[i][a]!=anc[i][b]) a=anc[i][a],b=anc[i][b];
    }
    if(a==b)return a;
    return anc[0][a];
}
ll dist(ll a,ll b){
    ll l=lca(a,b);
    return d[a]+d[b]-2*d[l];
}*/
//pair<ll,ll> p[10005];
/*ll a[100005];
ll bit[100005];
ll lmr[1000005];
ll pi[1000005];
ll ans[1000005];
void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}
pair<pair<ll,ll>,ll> >query[1000005];
*/

//pair<ll,ll> a[205];
//template for #trie
//vector<ll> adj[100005],adjr[100005];

//ll a[100005],dp[100005];
pair<ll,ll> edges[100005];
vector<ll> fino;
ll vis[100005];
vector<ll> adj[100005];
ll n;
void dfs(ll v){
	vis[v]=1;
	for(ll i=0;i<adj[v].size();i++) if(!vis[adj[v][i]]) dfs(adj[v][i]);
	fino.pb(v);
}
ll check(ll mid){

	ll i,j,k,l,s;
	ll indg[100005]={0},pr[100005]={0};
	fino.clear();
	for(i=1;i<=n;i++) adj[i].clear();
	memset(vis,0,sizeof(vis));
	for(i=1;i<=mid;i++) adj[edges[i].ff].pb(edges[i].ss),pr[edges[i].ff]=1,pr[edges[i].ss]=1,indg[edges[i].ss]++;
    for(i=1;i<=n;i++)if(indg[i]==0 && pr[i]) s=i;
    dfs(s);
    if(fino.size()==n) return 1;
    else return  0;
}
int main(){
	ll q,i,j,k,l,m;
    cin>>n>>m;
    for(i=1;i<=m;i++) cin>>edges[i].ff>>edges[i].ss;
    ll hi=m;
    ll lo=1;
    ll ans=mod;
    while(hi>=lo){
    	ll mid=(hi+lo)/2;
    	if(check(mid)) ans=min(ans,mid),hi=mid-1;
    	else lo=mid+1;
    }
    if(ans==mod)cout<<-1;
    else cout<<ans;
}


#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define MP make_pair
#define inf 0x3f3f3f3f
#define mod 1000000007
#define N 200010
#define M 400020

struct edge{
	int u, v;
	void input(){
		scanf("%d%d", &u, &v);
	}
};

edge E[N];
int fst[N], vv[M], nxt[M], e;
int in[N], fa[N];
bool vis[N];
void init(){
	memset(fst, -1, sizeof fst); e = 0;
}
void add(int u, int v){
	vv[e] = v, nxt[e] = fst[u], fst[u] = e++;
}
int find(int x){
	if(fa[x] != x) fa[x] = find(fa[x]);
	return fa[x];
}
int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	init();
	for(int i = 1; i <= m; ++i){
		E[i].input();
		add(E[i].u, E[i].v);
		in[E[i].v]++;
	}
	bool ok = 0;
	queue<int> q;
	int cnt = 0;
	for(int i = 1; i <= n; ++i){
		if(in[i] == 0){
			q.push(i);
			vis[i] = 1;
			cnt++;
		}
	}
	if(cnt >= 2 || cnt == 0) ok = 1;
	if(cnt == 1){
		while(!q.empty()){
			int u = q.front(); q.pop();
			for(int i = fst[u]; ~i; i = nxt[i]){
				int v = vv[i];
				in[v]--;
				if(in[v] == 0){
					vis[v] = 1;
					q.push(v);
				}
			}
		}
		for(int i = 1; i <= n; ++i){
			if(!vis[i])
				ok = 1;
		}
	}
	puts("-1");
	if(ok){
		puts("-1"); return 0;
	}

	for(int i = 1; i<= n; ++i) fa[i] = i;
	int ans = 0;
	for(int i = 1; i <= m; ++i){
		int u = E[i].u, v = E[i].v;
		if(find(u) == find(v)) continue;
		ans = i;
		fa[find(u)] = find(v);
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define MP make_pair
#define inf 0x3f3f3f3f
#define mod 1000000007
#define N 200010
#define M 400020

struct edge{
	int u, v;
	void input(){
		scanf("%d%d", &u, &v);
	}
};

edge E[N];
int fst[N], vv[M], nxt[M], e;
int n, m, in[N], dis[N];
void init(){
	for(int i = 0; i <= n; ++i) fst[i] = -1, in[i] = 0, dis[i] = inf;
	e = 0;
}
void add(int u, int v){
	vv[e] = v, nxt[e] = fst[u], fst[u] = e++;
}
bool check(int mid){
	init();
	for(int i = 1; i <= mid; ++i){
		add(E[i].u, E[i].v);
		in[E[i].v]++;
	}
	bool ok = 1;
	queue<int> q;
	int cnt = 0;
	for(int i = 1; i <= n; ++i){
		if(in[i] == 0){
			q.push(i);
			dis[i] = 0;
			cnt++;
		}
	}
	if(cnt >= 2) ok = 0;
	if(cnt == 1){
		while(!q.empty()){
			int u = q.front(); q.pop();
			for(int i = fst[u]; ~i; i = nxt[i]){
				int v = vv[i];
				in[v]--;
				if(in[v] == 0){
					dis[v] = dis[u] + 1;
					q.push(v);
				}
			}
		}
		bool flag = 0;
		for(int i = 1; i <= n; ++i){
			if(in[i])
				ok = 0;
			if(dis[i] == n - 1)
				flag = 1;
		}
		if(!flag) ok = 0;
	}
	return ok;
}
int main(){
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= m; ++i){
		E[i].input();
	}
	int L = 1, R = m;
	if(!check(R)){
		puts("-1"); return 0;
	}
	while(L < R){
		int mid = (L + R) >> 1;
		if(check(mid))
			R = mid;
		else L = mid + 1;
	}
	if(check(L)) printf("%d\n", L);
	else puts("-1");
	return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define MP make_pair
#define inf 0x3f3f3f3f
#define mod 1000000007
#define N 200010
#define M 400020

struct edge{
	int u, v;
	void input(){
		scanf("%d%d", &u, &v);
	}
};

edge E[N];
int fst[N], vv[M], nxt[M], e;
int n, m, in[N];
void init(){
	for(int i = 0; i <= n; ++i) fst[i] = -1, in[i] = 0;
	e = 0;
}
void add(int u, int v){
	vv[e] = v, nxt[e] = fst[u], fst[u] = e++;
}
bool check(int mid){
	init();
	for(int i = 1; i <= mid; ++i){
		add(E[i].u, E[i].v);
		in[E[i].v]++;
	}
	bool ok = 1;
	queue<int> q;
	int cnt = 0;
	for(int i = 1; i <= n; ++i){
		if(in[i] == 0){
			q.push(i);
			cnt++;
		}
	}
	if(cnt >= 2 || cnt == 0) ok = 0;
	if(cnt == 1){
		while(!q.empty()){
			int u = q.front(); q.pop();
			for(int i = fst[u]; ~i; i = nxt[i]){
				int v = vv[i];
				in[v]--;
				if(in[v] == 0){
					q.push(v);
				}
			}
		}
		for(int i = 1; i <= n; ++i)
			if(in[i])
				ok = 0;
	}
	return ok;
}
int main(){
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= m; ++i){
		E[i].input();
	}
	int L = 1, R = m;
	if(!check(R)){
		puts("-1"); return 0;
	}
	while(L < R){
		int mid = (L + R) >> 1;
		if(check(mid))
			R = mid;
		else L = mid + 1;
	}
	if(check(L)) printf("%d\n", L);
	else puts("-1");
	return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define MP make_pair
#define inf 0x3f3f3f3f
#define mod 1000000007
#define N 200010
#define M 400020

struct edge{
	int u, v;
	void input(){
		scanf("%d%d", &u, &v);
	}
};

edge E[N];
int fst[N], vv[M], nxt[M], e;
int n, m, in[N], fa[N];
bool vis[N];
void init(){
	for(int i = 0; i <= n; ++i) fst[i] = -1, vis[i] = 0, in[i] = 0;
	e = 0;
}
void add(int u, int v){
	vv[e] = v, nxt[e] = fst[u], fst[u] = e++;
}
int find(int x){
	if(fa[x] != x) fa[x] = find(fa[x]);
	return fa[x];
}
bool check(int mid){
	init();
	for(int i = 1; i <= mid; ++i){
		add(E[i].u, E[i].v);
		in[E[i].v]++;
	}
	bool ok = 0;
	queue<int> q;
	int cnt = 0;
	for(int i = 1; i <= n; ++i){
		if(in[i] == 0){
			q.push(i);
			vis[i] = 1;
			cnt++;
		}
	}
	if(cnt >= 2 || cnt == 0) ok = 1;
	if(cnt == 1){
		while(!q.empty()){
			int u = q.front(); q.pop();
			for(int i = fst[u]; ~i; i = nxt[i]){
				int v = vv[i];
				in[v]--;
				if(in[v] == 0){
					vis[v] = 1;
					q.push(v);
				}
			}
		}
		for(int i = 1; i <= n; ++i)
			if(!vis[i])
				ok = 1;
	}
	return !ok;
}
int main(){
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= m; ++i){
		E[i].input();
	}
	int L = 1, R = m;
	while(L < R){
		int mid = (L + R) >> 1;
		if(check(mid))
			R = mid;
		else L = mid + 1;
	}
	if(check(L)) printf("%d\n", L);
	else puts("-1");
	return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define MP make_pair
#define inf 0x3f3f3f3f
#define mod 1000000007
#define N 200010
#define M 400020

struct edge{
	int u, v;
	void input(){
		scanf("%d%d", &u, &v);
	}
};

edge E[N];
int fst[N], vv[M], nxt[M], e;
int in[N], fa[N];
bool vis[N];
void init(){
	memset(fst, -1, sizeof fst); e = 0;
}
void add(int u, int v){
	vv[e] = v, nxt[e] = fst[u], fst[u] = e++;
}
int find(int x){
	if(fa[x] != x) fa[x] = find(fa[x]);
	return fa[x];
}
int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	init();
	for(int i = 1; i <= m; ++i){
		E[i].input();
		add(E[i].u, E[i].v);
		in[E[i].v]++;
	}
	bool ok = 0;
	queue<int> q;
	int cnt = 0;
	for(int i = 1; i <= n; ++i){
		if(in[i] == 0){
			q.push(i);
			vis[i] = 1;
			cnt++;
		}
	}
	if(cnt >= 2 || cnt == 0) ok = 1;
	if(cnt == 1){
		while(!q.empty()){
			int u = q.front(); q.pop();
			for(int i = fst[u]; ~i; i = nxt[i]){
				int v = vv[i];
				in[v]--;
				if(in[v] == 0){
					vis[v] = 1;
					q.push(v);
				}
			}
		}
		for(int i = 1; i <= n; ++i){
			if(!vis[i])
				ok = 1;
		}
	}
	if(ok){
		puts("-1"); return 0;
	}

	for(int i = 1; i<= n; ++i) fa[i] = i;
	int ans = 0;
	for(int i = 1; i <= m; ++i){
		int u = E[i].u, v = E[i].v;
		if(find(u) == find(v)) continue;
		ans = i;
		fa[find(u)] = find(v);
	}
	printf("%d\n", ans);
	return 0;
}