#include<bits/stdc++.h>
//#include "grader.h"
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;
const int N = 1e5 + 10;
const int M = 1e9+7;
stack<int> s;
ll h[N], w[N], qs[N];
void solve() {
int n; cin >> n;
for (int i = 1; i <= n; ++i) cin >> h[i];
for (int i = 1; i <= n; ++i) cin >> w[i], qs[i] = qs[i-1]+w[i];
ll ans = 0;
s.push(0);
for (int i = 1; i <= n; ++i) {
while (h[s.top()] > h[i]) {
ll H = h[s.top()];
ll W = qs[i-1] - qs[s.top()-1];
s.pop();
H -= h[s.top()];
ans += ((H)*(H+1)/2)%M * (W*(W+1)/2)%M;
ans %= M;
ans += H*h[s.top()]%M * (W*(W+1)/2)%M;
ans %= M;
}
s.push(i);
}
while (s.size() > 1) {
ll H = h[s.top()];
ll W = qs[n] - qs[s.top()-1];
s.pop();
H -= h[s.top()];
ans += ((H)*(H+1)/2)%M * (W*(W+1)/2)%M;
ans %= M;
ans += H*h[s.top()]%M * (W*(W+1)/2)%M;
ans %= M;
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 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 |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
360 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |