Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 7
- 5 6 9 11 12 16 20
- 2 3 1 5 3 1 1
- _____ _ _ _ _
- |_ _| |__ ___ / \ _ __ ___| |__ _ _| |
- | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
- | | | | | | __// ___ \| | | \__ \ | | | |_| | |
- |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
- */
- #include<bits/stdc++.h>
- #define ll long long
- #define pb push_back
- #define ppb pop_back
- #define endl '\n'
- #define mii map<ll int,ll int>
- #define msi map<string,ll int>
- #define mis map<ll int, string>
- #define rep(i,a,b) for(ll int i=a;i<b;i++)
- #define mpi map<pair<ll int,ll int>,ll int>
- #define pii pair<ll int,ll int>
- #define vi vector<ll int>
- #define vii vector<pair<ll int, ll int>>
- #define vs vector<string>
- #define all(a) (a).begin(),(a).end()
- #define F first
- #define S second
- #define sz(x) (ll int)x.size()
- #define hell 1000000007
- #define lbnd lower_bound
- #define ubnd upper_bound
- #define bs binary_search
- #define mp make_pair
- #define what_is(x) cerr << #x << " is " << x << endl;
- #define time cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
- using namespace std;
- #define N 100005
- void solve()
- {
- ll n;
- cin>>n;
- vi a(n),b(n);
- rep(i,0,n)
- {
- cin>>a[i];
- }
- rep(i,0,n)
- {
- cin>>b[i];
- }
- stack<pii>st;
- st.push({0,b[0]+a[0]});
- vi ans(n,-1);
- rep(i,1,n)
- {
- if(st.top().S>=a[i])
- {
- pii tmp = st.top();
- st.pop();
- if(i!=n-1 && a[i]+b[i]<a[i+1])
- ans[i]=a[i]+b[i];
- tmp.S=max(tmp.S,a[i]+b[i]);
- st.push(tmp);
- }
- else
- {
- if(i!=n-1 && a[i]+b[i]<a[i+1])
- ans[i]=a[i]+b[i];
- else
- st.push({i,a[i]+b[i]});
- }
- }
- // while(!st.empty())
- // {
- // cout<<st.top().F<<" "<<st.top().S<<endl;
- // st.pop();
- // }
- for(ll i=n-1;i>=0;i--)
- {
- if(ans[i]==-1)
- ans[i]=st.top().S;
- if(st.top().F==i)
- {
- st.pop();
- }
- }
- rep(i,0,n)
- cout<<ans[i]<<" ";
- return;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int TESTS=1;
- // cin>>TESTS;
- while(TESTS--)
- {
- solve();
- }
- time
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment