#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define pb push_back
#define endl '\n'
#define Mp make_pair
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("connect.in" , "r" , stdin) ; freopen("connect.out" , "w" , stdout);
ll mod = 1e9+7;
ll power(ll a, ll b){
ll ans=1;
a%=mod;
while(b){
if(b&1) ans=ans*a%mod;
b>>=1;
a=a*a%mod;
}
return ans;
}
const int N = 1e5+23;
ll pref[N], L[N], R[N];
ll h[N];
ll H[N], W[N], w[N];
int n;
map<pll, int> vis;
ll C(ll n){
n%=mod;
return n*(n+1)%mod*power(2, mod-2)%mod;
}
int main()
{
fast_io
cin>>n;
for (int i=1; i<=n; i++) cin>>h[i];
for (int i=1; i<=n; i++) cin>>w[i];
for (int i=1; i<=n+1; i++) pref[i]=pref[i-1]+w[i];
vector<int> st;
for (int i=1; i<=n; i++){
while(!st.empty() && h[st.back()]>=h[i]) st.pop_back();
if(!st.empty()) L[i]=st.back();
st.pb(i);
}
st.clear();
for (int i=n; i>0; i--){
while(!st.empty() && h[st.back()]>=h[i]) st.pop_back();
if(!st.empty()) R[i]=st.back();
else R[i]=n+1;
st.pb(i);
}
ll ans=0;
for (int i=1; i<=n; i++){
if(vis[{h[i], L[i]}]) continue;
vis[{h[i], L[i]}]=1;
ll tool=pref[R[i]-1]-pref[L[i]];
ll r1=h[i]-max(h[R[i]], h[L[i]]);
ll r2=max(h[R[i]], h[L[i]]);
ans=(ans+(C(r1)%mod+ r1*r2%mod)%mod*C(tool)%mod)%mod;
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
22 ms |
6236 KB |
Output is correct |
4 |
Correct |
37 ms |
7616 KB |
Output is correct |
5 |
Correct |
41 ms |
8184 KB |
Output is correct |
6 |
Correct |
26 ms |
7232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
9 ms |
5896 KB |
Output is correct |
4 |
Correct |
23 ms |
6788 KB |
Output is correct |
5 |
Correct |
22 ms |
6740 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
9 ms |
5768 KB |
Output is correct |
5 |
Correct |
19 ms |
6724 KB |
Output is correct |
6 |
Correct |
18 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
2396 KB |
Output is correct |
8 |
Correct |
13 ms |
3412 KB |
Output is correct |
9 |
Correct |
29 ms |
7252 KB |
Output is correct |
10 |
Correct |
109 ms |
13764 KB |
Output is correct |
11 |
Correct |
110 ms |
13780 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2516 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2772 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
20 ms |
6236 KB |
Output is correct |
12 |
Correct |
33 ms |
7508 KB |
Output is correct |
13 |
Correct |
40 ms |
7940 KB |
Output is correct |
14 |
Correct |
32 ms |
7004 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
9 ms |
5860 KB |
Output is correct |
18 |
Correct |
18 ms |
6748 KB |
Output is correct |
19 |
Correct |
17 ms |
6728 KB |
Output is correct |
20 |
Correct |
2 ms |
2392 KB |
Output is correct |
21 |
Correct |
11 ms |
3420 KB |
Output is correct |
22 |
Correct |
29 ms |
7236 KB |
Output is correct |
23 |
Correct |
113 ms |
13712 KB |
Output is correct |
24 |
Correct |
109 ms |
13652 KB |
Output is correct |
25 |
Correct |
1 ms |
2400 KB |
Output is correct |
26 |
Correct |
1 ms |
2524 KB |
Output is correct |
27 |
Correct |
2 ms |
2400 KB |
Output is correct |
28 |
Correct |
2 ms |
2400 KB |
Output is correct |
29 |
Correct |
2 ms |
2400 KB |
Output is correct |
30 |
Correct |
12 ms |
3424 KB |
Output is correct |
31 |
Correct |
12 ms |
3424 KB |
Output is correct |
32 |
Correct |
56 ms |
8792 KB |
Output is correct |
33 |
Correct |
70 ms |
8968 KB |
Output is correct |
34 |
Correct |
128 ms |
13216 KB |
Output is correct |
35 |
Correct |
114 ms |
12880 KB |
Output is correct |
36 |
Correct |
121 ms |
13420 KB |
Output is correct |
37 |
Correct |
132 ms |
13380 KB |
Output is correct |
38 |
Correct |
0 ms |
2392 KB |
Output is correct |
39 |
Correct |
115 ms |
12904 KB |
Output is correct |
40 |
Correct |
110 ms |
13380 KB |
Output is correct |
41 |
Correct |
108 ms |
13392 KB |
Output is correct |
42 |
Correct |
110 ms |
13084 KB |
Output is correct |