-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathMonster Game I.cpp
65 lines (55 loc) · 1.34 KB
/
Monster Game I.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 2e5+1;
const int SIZE = 4e6;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct Line {
ll m, b;
ll operator()(const ll x) const {
return m * x + b;
}
} seg[SIZE];
int N, lo[SIZE], hi[SIZE];
ll X, s[maxN], f[maxN];
void build(int i, int l, int r){
lo[i] = l; hi[i] = r;
seg[i] = {0, INF};
if(l == r) return;
int m = (l+r)/2;
build(2*i, l, m);
build(2*i+1, m+1, r);
}
void insert(int i, Line L){
int l = lo[i], r = hi[i];
if(l == r){
if(L(l) < seg[i](l))
seg[i] = L;
return;
}
int m = (l+r)/2;
if(seg[i].m < L.m) swap(seg[i], L);
if(seg[i](m) > L(m)){
swap(seg[i], L);
insert(2*i, L);
} else insert(2*i+1, L);
}
ll query(int i, ll x){
int l = lo[i], r = hi[i];
if(l == r) return seg[i](x);
int m = (l+r)/2;
if(x < m) return min(seg[i](x), query(2*i, x));
else return min(seg[i](x), query(2*i+1, x));
}
int main(){
scanf("%d %lld", &N, &X);
for(int i = 0; i < N; i++) scanf("%lld", &s[i]);
for(int i = 0; i < N; i++) scanf("%lld", &f[i]);
build(1, 1, 1e6);
insert(1, {X, 0});
for(int i = 0; i < N-1; i++){
ll best = query(1, s[i]);
insert(1, {f[i], best});
}
printf("%lld\n", query(1, s[N-1]));
}