#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define mii map<int,int>
#define F first
#define S second
#define pb push_back
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#pragma GCC optimize ("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-O2")
const int N = 1e5+5, mod = 1e9+7;
int h[N], w[N], ans[N], ps[N];
vector <int> v;
int inv2;
int pw(int a, int b)
{
if (b == 0)
return 1;
if (b % 2 == 1)
return (pw(a * a % mod, b/2) % mod) * a % mod;
return pw(a * a % mod, b/2) % mod;
}
int c2(int x)
{
x %= mod;
return (x * (x-1+mod) % mod) * inv2 % mod;
}
int tr(int hr, int wr)
{
hr %= mod, wr %= mod;
int a;
a = (((c2(hr * wr % mod) + (hr * c2(wr))) % mod) + (wr * c2(hr))) % mod;
a = (a * inv2) % mod;
return (a + (hr * wr%mod))%mod;
}
int32_t main()
{
fast
int n;
cin >> n;
inv2 = pw(2,mod-2);
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 1; i <= n; i++)
cin >> w[i], ps[i] = ps[i-1] + w[i], ps[i] %= mod;
h[0] = 0;
v.pb(0);
int sum = 0;
for (int i = 1; i <= n; i++)
{
while (h[v[v.size()-1]]>=h[i])
v.pop_back();
int j = v[v.size()-1];
v.pb(i);
int tw = (ps[i] - ps[j] + mod)%mod;
ans[i] = (tr(tw,h[i]) - tr(tw-w[i]+mod,h[i])+mod);
ans[i] %= mod;
ans[i] += (w[i]*ans[j])%mod;
ans[i] %= mod;
sum += ans[i];
sum %= mod;
ans[i] = ((c2(h[i]) + h[i])%mod * tw)%mod + ans[j];
ans[i] %= mod;
}
cout << sum << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 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 |
9 ms |
3672 KB |
Output is correct |
4 |
Correct |
19 ms |
4700 KB |
Output is correct |
5 |
Correct |
19 ms |
4692 KB |
Output is correct |
6 |
Correct |
19 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Correct |
11 ms |
2908 KB |
Output is correct |
4 |
Correct |
22 ms |
3416 KB |
Output is correct |
5 |
Correct |
22 ms |
3588 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
11 ms |
3164 KB |
Output is correct |
5 |
Correct |
21 ms |
3416 KB |
Output is correct |
6 |
Correct |
22 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
3 ms |
2896 KB |
Output is correct |
9 |
Correct |
11 ms |
4444 KB |
Output is correct |
10 |
Correct |
22 ms |
6364 KB |
Output is correct |
11 |
Correct |
22 ms |
6500 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2552 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2528 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
2516 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
10 ms |
3464 KB |
Output is correct |
12 |
Correct |
18 ms |
4700 KB |
Output is correct |
13 |
Correct |
19 ms |
4688 KB |
Output is correct |
14 |
Correct |
18 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
3 ms |
2780 KB |
Output is correct |
17 |
Correct |
13 ms |
3928 KB |
Output is correct |
18 |
Correct |
22 ms |
5532 KB |
Output is correct |
19 |
Correct |
23 ms |
5584 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
2844 KB |
Output is correct |
22 |
Correct |
12 ms |
4468 KB |
Output is correct |
23 |
Correct |
22 ms |
6480 KB |
Output is correct |
24 |
Correct |
23 ms |
6448 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
3 ms |
2808 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
11 ms |
3928 KB |
Output is correct |
33 |
Correct |
12 ms |
4060 KB |
Output is correct |
34 |
Correct |
22 ms |
5212 KB |
Output is correct |
35 |
Correct |
22 ms |
5248 KB |
Output is correct |
36 |
Correct |
23 ms |
5452 KB |
Output is correct |
37 |
Correct |
23 ms |
5468 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
22 ms |
5372 KB |
Output is correct |
40 |
Correct |
25 ms |
5468 KB |
Output is correct |
41 |
Correct |
22 ms |
5716 KB |
Output is correct |
42 |
Correct |
22 ms |
6152 KB |
Output is correct |