#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod (long long)1000000007
ll sum(ll n) {
return (n*(n+1))/2;
}
ll multiply(ll a, ll b) {
a%=mod;
b%=mod;
return (a*b)%mod;
}
int main () {
ll n, ans = 0;
cin >> n;
ll h[n+1] = {0}, w[n+1] = {0}, pref[n+1] = {0}, dp[n+1] = {0};
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;i++) {
pref[i] = pref[i-1]+w[i];
}
stack<ll> s;
s.push(0);
for(int i = 1;i<=n;i++) {
while(!s.empty() and h[s.top()] >= h[i])s.pop();
dp[i] = dp[s.top()]+multiply(sum(h[i]), (pref[i] - pref[s.top()]));
ans += multiply(dp[s.top()], w[i]) + multiply(sum(h[i]), multiply(pref[i-1] - pref[s.top()], w[i]))+multiply(sum(h[i]), sum(w[i]));
dp[i]%=mod;
ans%=mod;s.push(i);
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
43 ms |
2372 KB |
Output is correct |
4 |
Correct |
65 ms |
4536 KB |
Output is correct |
5 |
Correct |
65 ms |
4528 KB |
Output is correct |
6 |
Correct |
65 ms |
4548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
10 ms |
688 KB |
Output is correct |
3 |
Correct |
46 ms |
2712 KB |
Output is correct |
4 |
Correct |
92 ms |
5304 KB |
Output is correct |
5 |
Correct |
96 ms |
5428 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
10 ms |
716 KB |
Output is correct |
4 |
Correct |
47 ms |
2724 KB |
Output is correct |
5 |
Correct |
91 ms |
5304 KB |
Output is correct |
6 |
Correct |
95 ms |
5440 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
9 ms |
792 KB |
Output is correct |
9 |
Correct |
50 ms |
2964 KB |
Output is correct |
10 |
Correct |
88 ms |
5960 KB |
Output is correct |
11 |
Correct |
104 ms |
6072 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
292 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
33 ms |
2348 KB |
Output is correct |
12 |
Correct |
79 ms |
4564 KB |
Output is correct |
13 |
Correct |
66 ms |
4540 KB |
Output is correct |
14 |
Correct |
65 ms |
4532 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
10 ms |
788 KB |
Output is correct |
17 |
Correct |
46 ms |
2712 KB |
Output is correct |
18 |
Correct |
91 ms |
5304 KB |
Output is correct |
19 |
Correct |
98 ms |
5432 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
10 ms |
844 KB |
Output is correct |
22 |
Correct |
48 ms |
2964 KB |
Output is correct |
23 |
Correct |
88 ms |
5948 KB |
Output is correct |
24 |
Correct |
90 ms |
6084 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
304 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
10 ms |
796 KB |
Output is correct |
31 |
Correct |
10 ms |
716 KB |
Output is correct |
32 |
Correct |
45 ms |
2600 KB |
Output is correct |
33 |
Correct |
46 ms |
2728 KB |
Output is correct |
34 |
Correct |
86 ms |
5060 KB |
Output is correct |
35 |
Correct |
85 ms |
5064 KB |
Output is correct |
36 |
Correct |
93 ms |
5304 KB |
Output is correct |
37 |
Correct |
93 ms |
5348 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
106 ms |
5316 KB |
Output is correct |
40 |
Correct |
91 ms |
5312 KB |
Output is correct |
41 |
Correct |
96 ms |
5348 KB |
Output is correct |
42 |
Correct |
91 ms |
5692 KB |
Output is correct |