#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1e9+7, INV = 250000002;
int f(int n, int m) {
return n * m % MOD * (n + 1) % MOD * (m + 1) % MOD * INV % MOD;
}
signed main() {
//input and sum
int n;
cin >> n;
vector<int> h(n), w(n);
for(int i = 0; i < n; i++) cin >> h[i];
for(int i = 0; i < n; i++) cin >> w[i];
//linearily go over and compute result
int res = 0;
vector<int> hSorted(n);
for(int i = 0; i < n; i++) hSorted[i] = h[i];
sort(hSorted.begin(), hSorted.end());
for(int i = 0; i < n; i++) {
int consecutiveW = 0, minH = INT_MAX;
for(int j = 0; j < n; j++) {
if(h[j] < hSorted[i]) {
res = (res + f(minH, consecutiveW)) % MOD;
if(i != 0) res = (res - f(hSorted[i-1], consecutiveW)) % MOD;
if(res < 0) res = (res + 10000000070000000) % MOD;
consecutiveW = 0, minH = INT_MAX;
} else {
consecutiveW = (consecutiveW + w[j]) % MOD;
minH = min(minH, h[j]);
}
}
res = (res + f(minH, consecutiveW)) % MOD;
if(i != 0) res = (res - f(hSorted[i-1], consecutiveW)) % MOD;
if(res < 0) res = (res + 10000000070000000) % MOD;
}
//cout << h[n-1] << " " << m << "\n";
cout << res << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
13 ms |
476 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
8 ms |
468 KB |
Output is correct |
3 |
Execution timed out |
1026 ms |
2128 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
373 ms |
860 KB |
Output is correct |
3 |
Execution timed out |
1056 ms |
2356 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
5 ms |
348 KB |
Output is correct |
3 |
Correct |
373 ms |
860 KB |
Output is correct |
4 |
Execution timed out |
1068 ms |
2432 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
14 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
13 ms |
476 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |