-
Notifications
You must be signed in to change notification settings - Fork 154
/
2084 - Monster Game I.cpp
50 lines (47 loc) · 1.26 KB
/
2084 - 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
/*
Problem Name: Monster Game I
Problem Link: https://cses.fi/problemset/task/2084
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define F first
#define S second
signed main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n, k; cin >> n >> k;
vector<pair<int, int>> S = {{k, 0}};
vector<int> v = {1};
int s[n], f[n];
for (auto &i: s)
cin >> i;
for (auto &i: f)
cin >> i;
int c = 0;
for (int i = 0; i < n; i++) {
int x = s[i];
int it = upper_bound(v.begin(), v.end(), x) - v.begin() - 1;
int m = f[i];
c = x*S[it].F + S[it].S;
if (S.back().F == m) continue; // if same slope then always bad line
while (S.size() >= 2) {
auto y2 = S[S.size() - 1];
auto y3 = S[S.size() - 2];
if ((y2.S - c)*(m - y3.F) < (y3.S - c)*(m - y2.F)) {
S.pop_back();
v.pop_back();
}
else break;
}
int x1 = ceil((S.back().S - c)*1.0/(m - S.back().F));
v.push_back(x1);
S.push_back({m, c});
}
cout << c;
}