#include <bits/stdc++.h>
using namespace std;
// Nome: Kalila and Dimna in the Logging Industry
// f(i) = f(j) + ai * bj
// b is decreasing
// a is increasing
typedef long long ll;
struct line{ll slope, y0;};
ll n, a[100010], b[100010];
deque <line> hull;
ll evaluate(line l, ll x){
return l.slope * x + l.y0;
}
ll intersectionx(line a, line b){
return (a.y0 - b.y0) / (b.slope - a.slope);
}
line query(ll x){
while(hull.size() > 1){
if(evaluate(hull[0], x) < evaluate(hull[1], x)) break;
hull.pop_front();
}
return hull.front();
}
void update(line l){
ll s = hull.size();
while(s > 1){
ll x1 = intersectionx(l, hull[s-1]);
ll x2 = intersectionx(hull[s-2], hull[s-1]);
if(x1 > x2) break;
hull.pop_back();
s--;
}
hull.push_back(l);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(ll i=0;i<n;i++) cin >> a[i];
for(ll i=0;i<n;i++) cin >> b[i];
ll f[n];
f[0] = 0;
hull.push_back({b[0], f[0]});
for(ll i=1;i<n;i++){
f[i] = evaluate(query(a[i]), a[i]);
update({b[i], f[i]});
}
cout << f[n-1] << '\n';
return 0;
}
1047. Remove All Adjacent Duplicates In String | 977. Squares of a Sorted Array |
852. Peak Index in a Mountain Array | 461. Hamming Distance |
1748. Sum of Unique Elements | 897. Increasing Order Search Tree |
905. Sort Array By Parity | 1351. Count Negative Numbers in a Sorted Matrix |
617. Merge Two Binary Trees | 1450. Number of Students Doing Homework at a Given Time |
700. Search in a Binary Search Tree | 590. N-ary Tree Postorder Traversal |
589. N-ary Tree Preorder Traversal | 1299. Replace Elements with Greatest Element on Right Side |
1768. Merge Strings Alternately | 561. Array Partition I |
1374. Generate a String With Characters That Have Odd Counts | 1822. Sign of the Product of an Array |
1464. Maximum Product of Two Elements in an Array | 1323. Maximum 69 Number |
832. Flipping an Image | 1295. Find Numbers with Even Number of Digits |
1704. Determine if String Halves Are Alike | 1732. Find the Highest Altitude |
709. To Lower Case | 1688. Count of Matches in Tournament |
1684. Count the Number of Consistent Strings | 1588. Sum of All Odd Length Subarrays |
1662. Check If Two String Arrays are Equivalent | 1832. Check if the Sentence Is Pangram |