#include <bits/stdc++.h>
using namespace std;
#define int ll
#define endl '\n' //comment for interactive
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl << flush
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
typedef array<int, 3> ti;
typedef vector<ii> vii;
typedef vector<ti> vti;
typedef vector<vi> vvi;
typedef priority_queue<int> pq;
template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }
const int inf = 1e17;
const int mod = 1e9+7;
int power(ll x, int y) {
int res = 1;
x = x % mod;
while(y > 0) {
if(y & 1)
res = ((ll)res*x) % mod;
y = y >> 1;
x = (x*x) % mod;
}
return res;
}
int inv(int n) {
return power(n, mod-2);
}
int mm(int x, int y) {
x %= mod, y %= mod;
return (x * y) % mod;
}
int nc2(int n) {
return mm(mm(n, (n-1)), inv(2));
}
void solve() {
int n;
cin >> n;
vi h(n+1), w(n+1);
loop1(i, n)
cin >> h[i];
loop1(i, n)
cin >> w[i];
vi pw(n+1);
loop1(i, n) {
pw[i] = pw[i-1] + w[i];
pw[i] %= mod;
}
auto calc = [&] (int l, int r, auto calc) -> int {
if(r < l) return 0;
ii minh = {inf, inf};
for(int i = l; i <= r; i++)
ckmin(minh, {h[i], i});
auto [mh, mi] = minh;
return
(mm(nc2(mh+1), nc2(w[mi] + 1))
+ mm(nc2(mh+1), pw[mi-1] - pw[l-1])
+ mm(nc2(mh+1), pw[r] - pw[mi])
+ calc(l, mi-1, calc)
+ calc(mi+1, r, calc)) % mod;
};
cout << ((calc(1, n, calc) + mod) % mod) << endl;
}
signed main() {
fast_io;
int t = 1; //cin >> t;
while(t--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
356 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
2 ms |
352 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
356 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |