#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int md = 1e9 + 7;
struct mint{
int v;
mint() : v(0) {}
mint(ll v_) : v(int(v_ % md)){ if (v < 0) v += md; }
explicit operator int() const{ return v; }
friend std::ostream& operator << (std::ostream& out, const mint& n){ return out << int(n); }
friend std::istream& operator >> (std::istream& in, mint& n){ ll v_; in >> v_; n = mint(v_); return in; }
friend bool operator == (const mint& a, const mint& b){ return a.v == b.v; }
friend bool operator != (const mint& a, const mint& b){ return a.v != b.v; }
friend bool operator < (const mint& a, const mint& b){ return a.v < b.v; }
friend bool operator <= (const mint& a, const mint& b){ return a.v <= b.v; }
friend bool operator > (const mint& a, const mint& b){ return a.v > b.v; }
friend bool operator >= (const mint& a, const mint& b){ return a.v >= b.v; }
mint& operator += (const mint& o){ ((v += o.v) >= md) ? v -= md : 0; return *this; }
mint& operator -= (const mint& o){ ((v -= o.v) < 0) ? v += md : 0; return *this; }
mint& operator *= (const mint& o){ v = int((ll)v * o.v % md); return *this; }
mint& operator /= (const mint& o){ return (*this) *= inv(o); }
mint operator - () const{ return mint(-v); }
mint& operator ++ (){ return *this += 1; }
mint& operator -- (){ return *this -= 1; }
friend mint operator + (mint a, const mint& b){ return a += b; }
friend mint operator - (mint a, const mint& b){ return a -= b; }
friend mint operator * (mint a, const mint& b){ return a *= b; }
friend mint operator / (mint a, const mint& b){ return a /= b; }
friend mint pow(mint a, ll p){ return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1); }
friend mint inv(const mint& a){ return pow(a, md - 2); }
};
mint sum(mint x){ return x * (x + 1) / 2; }
int main(){
int n; cin >> n;
mint h[n + 1], w[n + 1], p[n + 1], dp[n + 1], ans;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) cin >> w[i];
for (int i = 1; i <= n; i++) p[i] = p[i - 1] + w[i];
stack<int> stk; stk.push(0);
for (int i = 1; i <= n; i++){
while (!stk.empty() and h[stk.top()] >= h[i]) stk.pop();
dp[i] = dp[stk.top()] + sum(h[i]) * (p[i] - p[stk.top()]);
ans += dp[stk.top()] * w[i] + sum(h[i]) * (p[i - 1] - p[stk.top()]) * w[i] + sum(h[i]) * sum(w[i]);
stk.push(i);
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
288 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
288 KB |
Output is correct |
4 |
Correct |
0 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
71 ms |
1540 KB |
Output is correct |
4 |
Correct |
145 ms |
2992 KB |
Output is correct |
5 |
Correct |
143 ms |
2984 KB |
Output is correct |
6 |
Correct |
141 ms |
2972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
17 ms |
588 KB |
Output is correct |
3 |
Correct |
89 ms |
2056 KB |
Output is correct |
4 |
Correct |
165 ms |
3740 KB |
Output is correct |
5 |
Correct |
167 ms |
3868 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
296 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
17 ms |
640 KB |
Output is correct |
4 |
Correct |
85 ms |
1932 KB |
Output is correct |
5 |
Correct |
164 ms |
3744 KB |
Output is correct |
6 |
Correct |
166 ms |
3880 KB |
Output is correct |
7 |
Correct |
2 ms |
204 KB |
Output is correct |
8 |
Correct |
17 ms |
648 KB |
Output is correct |
9 |
Correct |
83 ms |
2048 KB |
Output is correct |
10 |
Correct |
160 ms |
4004 KB |
Output is correct |
11 |
Correct |
166 ms |
4164 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
292 KB |
Output is correct |
8 |
Correct |
2 ms |
308 KB |
Output is correct |
9 |
Correct |
2 ms |
204 KB |
Output is correct |
10 |
Correct |
2 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
2 ms |
204 KB |
Output is correct |
14 |
Correct |
2 ms |
204 KB |
Output is correct |
15 |
Correct |
2 ms |
296 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
272 KB |
Output is correct |
4 |
Correct |
2 ms |
308 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
2 ms |
296 KB |
Output is correct |
11 |
Correct |
70 ms |
1576 KB |
Output is correct |
12 |
Correct |
140 ms |
2884 KB |
Output is correct |
13 |
Correct |
142 ms |
2984 KB |
Output is correct |
14 |
Correct |
142 ms |
2964 KB |
Output is correct |
15 |
Correct |
2 ms |
204 KB |
Output is correct |
16 |
Correct |
18 ms |
636 KB |
Output is correct |
17 |
Correct |
81 ms |
1924 KB |
Output is correct |
18 |
Correct |
166 ms |
3732 KB |
Output is correct |
19 |
Correct |
165 ms |
3876 KB |
Output is correct |
20 |
Correct |
2 ms |
300 KB |
Output is correct |
21 |
Correct |
17 ms |
648 KB |
Output is correct |
22 |
Correct |
82 ms |
2084 KB |
Output is correct |
23 |
Correct |
159 ms |
4036 KB |
Output is correct |
24 |
Correct |
162 ms |
4116 KB |
Output is correct |
25 |
Correct |
1 ms |
292 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
2 ms |
296 KB |
Output is correct |
28 |
Correct |
2 ms |
304 KB |
Output is correct |
29 |
Correct |
2 ms |
304 KB |
Output is correct |
30 |
Correct |
18 ms |
580 KB |
Output is correct |
31 |
Correct |
16 ms |
616 KB |
Output is correct |
32 |
Correct |
80 ms |
1920 KB |
Output is correct |
33 |
Correct |
85 ms |
1928 KB |
Output is correct |
34 |
Correct |
159 ms |
3476 KB |
Output is correct |
35 |
Correct |
159 ms |
3476 KB |
Output is correct |
36 |
Correct |
167 ms |
3864 KB |
Output is correct |
37 |
Correct |
172 ms |
3732 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
166 ms |
3772 KB |
Output is correct |
40 |
Correct |
161 ms |
3736 KB |
Output is correct |
41 |
Correct |
164 ms |
3780 KB |
Output is correct |
42 |
Correct |
168 ms |
4000 KB |
Output is correct |