#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 26;
const ll inf = 1e15;
const ll mod = 1e9+7;
const ld ex = 1e-9;
int n, t, k, m, d[N], lab[N];
ll ans, tong, a[N], b[N], c[N], l[N], r[N], cnt;
string s;
struct node
{
int x, y, val;
int vx, vy;
node(){}
node(int _x, int _vx, int _y, int _vy)
{
x = _x;
vx = _vx;
y = _y;
vy = _vy;
}
};
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n&1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
ll C(ll u, ll v)
{
if(u > v)return 0;
return a[v] * b[u] % mod * b[v-u] % mod;
}
void add(ll& x, ll y)
{
x += y;
if(x >= mod)x -= mod;
}
vector<int> vi;
void sol()
{
cin >> n;
for(int i = 1; i <= n; i ++)cin >> a[i];
for(int i = 1; i <= n; i ++)
{
cin >> c[i];
b[i] = b[i-1] + c[i];
}
for(int i = 1; i <= n; i ++)
{
while(!vi.empty() && a[vi.back()] > a[i])vi.pop_back();
l[i] = vi.empty() ? 0 : vi.back();
vi.pb(i);
}
vi.clear();
for(int i = n; i > 0; i --)
{
while(!vi.empty() && a[vi.back()] >= a[i])vi.pop_back();
r[i] = vi.empty() ? n+1 : vi.back();
vi.pb(i);
}
for(int i = 1; i <= n; i ++)
{
tong = ((b[r[i]-1]-b[i])%mod*((b[i]-b[l[i]])%mod)%mod+c[i]*(c[i]+1)/2%mod+(b[i-1]-b[l[i]])%mod*c[i]%mod) % mod;
cnt = a[i] * (a[i]+1) % mod * pw(2, mod-2) % mod;
//cout << cnt <<" ";
ans = (ans + cnt * tong % mod) % mod;
//cout << cnt <<" "<<tong << '\n';
}
cout << ans;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
*/
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:93:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:94:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
15 ms |
2512 KB |
Output is correct |
4 |
Correct |
32 ms |
4692 KB |
Output is correct |
5 |
Correct |
32 ms |
4560 KB |
Output is correct |
6 |
Correct |
31 ms |
4324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
844 KB |
Output is correct |
3 |
Correct |
17 ms |
2640 KB |
Output is correct |
4 |
Correct |
37 ms |
4812 KB |
Output is correct |
5 |
Correct |
37 ms |
4776 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
6 ms |
844 KB |
Output is correct |
4 |
Correct |
18 ms |
2652 KB |
Output is correct |
5 |
Correct |
44 ms |
4684 KB |
Output is correct |
6 |
Correct |
37 ms |
4772 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
844 KB |
Output is correct |
9 |
Correct |
18 ms |
2636 KB |
Output is correct |
10 |
Correct |
41 ms |
4776 KB |
Output is correct |
11 |
Correct |
41 ms |
4772 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
0 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
332 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 |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
18 ms |
3020 KB |
Output is correct |
12 |
Correct |
31 ms |
5852 KB |
Output is correct |
13 |
Correct |
31 ms |
5652 KB |
Output is correct |
14 |
Correct |
31 ms |
5452 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
972 KB |
Output is correct |
17 |
Correct |
18 ms |
3548 KB |
Output is correct |
18 |
Correct |
39 ms |
6712 KB |
Output is correct |
19 |
Correct |
39 ms |
6724 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
4 ms |
972 KB |
Output is correct |
22 |
Correct |
19 ms |
3636 KB |
Output is correct |
23 |
Correct |
35 ms |
6548 KB |
Output is correct |
24 |
Correct |
35 ms |
6596 KB |
Output is correct |
25 |
Correct |
0 ms |
296 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
4 ms |
844 KB |
Output is correct |
31 |
Correct |
5 ms |
844 KB |
Output is correct |
32 |
Correct |
23 ms |
3088 KB |
Output is correct |
33 |
Correct |
19 ms |
3268 KB |
Output is correct |
34 |
Correct |
37 ms |
5944 KB |
Output is correct |
35 |
Correct |
35 ms |
5964 KB |
Output is correct |
36 |
Correct |
47 ms |
6228 KB |
Output is correct |
37 |
Correct |
37 ms |
6148 KB |
Output is correct |
38 |
Correct |
0 ms |
316 KB |
Output is correct |
39 |
Correct |
42 ms |
6076 KB |
Output is correct |
40 |
Correct |
35 ms |
6088 KB |
Output is correct |
41 |
Correct |
39 ms |
6208 KB |
Output is correct |
42 |
Correct |
37 ms |
6460 KB |
Output is correct |