#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 100005
struct info{
int w, h, id;
info(){}
info(int _w, int _h, int _id): w(_w), h(_h), id(_id){}
bool operator < (const info& o) const{
return h > o.h;
}
} A[MAX];
int power(int a, int exp){
int res = 1;
for (; exp > 0; exp >>= 1, a = (a * a) % Mod) if(exp & 1){
res = (res * a) % Mod;
}
return res;
}
const int inv4 = power(4, Mod - 2);
int pa[MAX], siz[MAX], nArr, vis[MAX];
int fp(int u){
return u == pa[u] ? u : pa[u] = fp(pa[u]);
}
int ans = 0;
int mul(int a, int b){
return 1LL * a * b % Mod;
}
int ask(int w, int h){
return mul(mul(mul(w, w + 1), mul(h, h + 1)), inv4);
}
void add(int &x, int y){
x += y;
if(x >= Mod) x %= Mod;
while(x < 0) x += Mod;
}
bool joint (int u, int v, int h){
u = fp(u), v = fp(v);
if (u == v) return false;
add(ans, -ask(siz[u], h));
add(ans, -ask(siz[v], h));
add(ans, ask(siz[u] + siz[v], h));
add(siz[u], siz[v]);
pa[v] = u;
return true;
}
void process(void){
cin >> nArr;
for (int i = 1; i <= nArr; ++i) cin >> A[i].h;
for (int i = 1; i <= nArr; ++i) cin >> A[i].w;
for (int i = 1; i <= nArr; ++i){
A[i].id = i;
pa[i] = i, siz[i] = A[i].w;
}
sort(A + 1, A + nArr + 1);
for (int i = 1; i <= nArr; ++i){
int h = A[i].h, id = A[i].id;
ans += ask(A[i].w, A[i].h);
if(vis[id + 1]) joint(id, id + 1, h);
if(vis[id - 1]) joint(id, id - 1, h);
vis[id] = true;
}
cout << ans;
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
// freopen(name".inp", "r", stdin);
// freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |