#include <iostream>
#include <vector>
#include <map>
#include <stack>
using namespace std;
const int mod=1e9+7;
#define int long long
signed main(){
int n;
cin>>n;
vector<int> v(n),h(n),pre(n+1);
map<int,int> mp;
for(int& i:h)cin>>i;
for(int& i:v)cin>>i;
for(int i=0;i<n;i++)pre[i+1]=(pre[i]+v[i])%mod;
stack<pair<int,int>> st;
st.push({0,n});
vector<pair<int,int>> comp(n);
for(int i=n-1;i>=0;i--){
while(st.top().first>=h[i])st.pop();
int dif=pre[st.top().second]-pre[i];
if(dif<0)dif+=mod;
comp[i]={h[i],dif};
st.push({h[i],i});
}
int ans=0;
for(int i=0;i<n;i++){
pair<int,int> x=comp[i];
//cout<<(1ll*x.first*(x.first+1)/2)<<' '<<(1ll*x.second*(x.second+1)/2)<<'\n';
ans+=(((x.first*(x.first+1)/2)%mod)*((x.second*(x.second+1)/2)%mod))%mod;
ans%=mod;
if(i){
pair<int,int> y=comp[i-1];
ans-=(((y.first*(y.first+1)/2)%mod)*((x.second*(x.second+1)/2)%mod))%mod;
if(ans<0)ans=((ans%mod)+mod)%mod;
}
}
//cout<<(((1ll*(mod-7)*(mod-6)/2)%mod)*((1ll*(mod-7)*(mod-6)/2)%mod))%mod<<' ';
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
336 KB |
Output is correct |
2 |
Correct |
7 ms |
592 KB |
Output is correct |
3 |
Correct |
29 ms |
2372 KB |
Output is correct |
4 |
Correct |
63 ms |
4324 KB |
Output is correct |
5 |
Correct |
59 ms |
4172 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
8 ms |
592 KB |
Output is correct |
4 |
Correct |
43 ms |
2376 KB |
Output is correct |
5 |
Correct |
56 ms |
4324 KB |
Output is correct |
6 |
Correct |
57 ms |
4312 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
6 ms |
592 KB |
Output is correct |
9 |
Correct |
29 ms |
2400 KB |
Output is correct |
10 |
Correct |
56 ms |
4168 KB |
Output is correct |
11 |
Correct |
52 ms |
4180 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |