#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 >> b[i];
c[i] = b[i];
b[i] += b[i-1];
}
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+c[i]*(c[i]+1)/2%mod+(b[i-1]-b[l[i]])%mod*c[i]%mod) % mod;
cnt = a[i] * (a[i]+1) / 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: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".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:95:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 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 |
9 ms |
3020 KB |
Output is correct |
4 |
Correct |
18 ms |
5828 KB |
Output is correct |
5 |
Correct |
20 ms |
5672 KB |
Output is correct |
6 |
Incorrect |
20 ms |
5516 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
968 KB |
Output is correct |
3 |
Correct |
12 ms |
3532 KB |
Output is correct |
4 |
Correct |
22 ms |
6724 KB |
Output is correct |
5 |
Correct |
27 ms |
6724 KB |
Output is correct |
6 |
Correct |
0 ms |
324 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 |
3 ms |
972 KB |
Output is correct |
4 |
Correct |
11 ms |
3588 KB |
Output is correct |
5 |
Correct |
29 ms |
6732 KB |
Output is correct |
6 |
Correct |
25 ms |
6828 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
972 KB |
Output is correct |
9 |
Correct |
13 ms |
3596 KB |
Output is correct |
10 |
Correct |
23 ms |
6480 KB |
Output is correct |
11 |
Correct |
22 ms |
6656 KB |
Output is correct |
12 |
Correct |
0 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |