// Only GOD
// believe in yourself
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define bit(x, y) ((x >> y)&1)
#define sz(x) (int)x.size()
#define kill(x) return cout << x << '\n', void()
#define KILL(x) return cout << x << '\n', 0
#define int ll
const int N = 2e5+10;
const int INF = 1e9;
const int MOD = 1e9+7;
int mul(int a, int b){
a %= MOD;
b %= MOD;
return (a*b)%MOD;
}
int add(int a, int b){
return (a+b+MOD)%MOD;
}
int dp[N], h[N], w[N];
int inv;
int f(int n){
return mul(mul(n, n+1), inv);
}
int pw(int a, int b){
int res = 1;
while(b){
if(b & 1)
res = mul(a, res);
a = mul(a, a);
b >>= 1;
}
return res;
}
int solve(int x, int len, int d){
int res = add(mul(2,x),mul(len-1,d));
res = mul(res, len);
return mul(res, inv);
}
int solve2(int x, int y){
return mul(x, f(y));
}
int32_t main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin >> n;
inv = pw(2, MOD-2);
for(int i = 1; i <= n; i++){
cin >> h[i];
}
for(int i = 1; i <= n;i++)
cin >> w[i];
int ans = 0;
if(n <= 1000){
for(int i = 1; i <= n; i++){
int mn = h[i];
dp[i] = mul(w[i], f(h[i]));
dp[i] = add(dp[i], mul(f(w[i]-1), f(h[i])));
for(int j = i-1; j >= 1; j--){
mn = min(mn, h[j]);
dp[i] = add(dp[i], mul(w[j],mul(w[i], f(mn))));
}
ans = add(ans, dp[i]);
}
KILL(ans);
}
else if(is_sorted(h+1, h+n+1)){
for(int i = 1; i <= n; i++){
dp[i] = mul(w[i], f(h[i]));
dp[i] = add(dp[i], mul(f(w[i]-1), f(h[i])));
dp[i] = add(dp[i], mul(dp[i-1], w[i]));
ans = add(ans, dp[i]);
}
KILL(ans);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
8 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Incorrect |
10 ms |
1108 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
340 KB |
Output is correct |
2 |
Incorrect |
4 ms |
476 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
360 KB |
Output is correct |
3 |
Incorrect |
3 ms |
468 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
368 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
340 KB |
Output is correct |
9 |
Correct |
8 ms |
340 KB |
Output is correct |
10 |
Correct |
11 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
9 ms |
368 KB |
Output is correct |
14 |
Correct |
8 ms |
340 KB |
Output is correct |
15 |
Correct |
8 ms |
368 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
8 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
8 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
8 ms |
340 KB |
Output is correct |
11 |
Incorrect |
7 ms |
1008 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |