#include <bits/stdc++.h>
using namespace std;
#define pii pair<ll,ll>
#define REP(i,x,y) for(ll i=x;i<=y;i++)
#define freeopen freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define mod 1000000007
#define pb push_back
#define mk make_pair
#define ll long long
#define foor(x,vec) for(auto x:vec ){cout<<x<<" ";}
#define fi first
#define se second
#define MAXN 700069
#define lld long double
#define cha ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ffl fflush(stdout)
#define sst string
#define pii pair<ll,ll>
ll mvx[]={0,0,-1,1};
ll mvy[]={1,-1,0,0};
ll n,q;
ll a,b;
ll h[MAXN],w[MAXN];
ll ps[MAXN];
ll dp[MAXN];
ll idx[MAXN];
ll mul(ll a,ll b){
a=a%mod;
b=b%mod;
a*=b;
a%=mod;
return a;
}
int main(){
cin>>n;
REP(i,1,n)cin>>h[i];
REP(i,1,n)cin>>w[i];
REP(i,1,n){
ps[i]=ps[i-1]+w[i];
}
stack <ll> stck;
REP(i,1,n){
while(stck.size() && h[i]<h[stck.top()]){
stck.pop();
}
if(stck.size())
idx[i]=stck.top();
else idx[i]=0;
stck.push(i);
}
ll ans=0;
REP(i,1,n){
ll val=(dp[idx[i]])%mod*(w[i]);
// ini buat sebelumnya trus gabung
val%=mod;
val=val+mul(h[i]*(h[i]+1)/2,w[i]*(w[i]+1)/2)%mod;
// buat sendiri
val%=mod;
val=val+(w[i]*mul(mul(h[i],h[i]+1)/2,(ps[i-1]-ps[idx[i]]))%mod)%mod;
// buat kalo ada yang sebelumnya juga
val%=mod;
ans+=val;
ans%=mod;
dp[i]=dp[idx[i]]+mul(mul(h[i],h[i]+1)/2,(ps[i]-ps[idx[i]]));
dp[i]%=mod;
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
18 ms |
10152 KB |
Output is correct |
4 |
Correct |
38 ms |
11960 KB |
Output is correct |
5 |
Correct |
37 ms |
11604 KB |
Output is correct |
6 |
Correct |
37 ms |
11336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
9 ms |
5060 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
7 ms |
4956 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |