#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
typedef long long ll;
typedef double dd;
struct Line
{
char type;
dd x;
ll m, b;
};
typedef set<Line>::iterator sit;
ll h[maxn], w[maxn];
ll dp[maxn];
set<Line> env;
bool operator< (const Line &a, const Line &b)
{
if (a.type+b.type > 0) return a.x < b.x;
return a.m > b.m;
}
bool hasPrev(sit it)
{
return (it != env.begin());
}
bool hasNext(sit it)
{
return (next(it) != env.end());
}
dd intersect(Line l1, Line l2)
{
return (dd)(l2.b-l1.b)/(l1.m-l2.m);
}
bool bad(Line l1, Line l2, Line l3)
{
return intersect(l1, l3) < intersect(l1, l2);
}
void get_x(sit it)
{
if (hasPrev(it))
{
Line l = *it;
l.x = intersect(*prev(it), *it);
env.erase(it); env.insert(l);
}
}
void add(ll m, ll b)
{
Line l = {0, 0, m, b};
sit it = env.lower_bound(l);
if (it != env.end() && it->m == l.m)
{
if (it->b <= l.b) return;
env.erase(it);
}
env.insert(l);
it = env.find(l);
if (hasPrev(it) && hasNext(it) && bad(*prev(it), *it, *next(it)))
{
env.erase(it);
return;
}
while (hasNext(it) && hasNext(next(it)) && bad(*it, *next(it), *next(next(it))))
env.erase(next(it));
while (hasPrev(it) && hasPrev(prev(it)) && bad(*prev(prev(it)), *prev(it), *it))
env.erase(prev(it));
get_x(it);
if (hasNext(it)) get_x(next(it));
}
ll query(ll x)
{
sit it = env.upper_bound({1, (dd)x, 0, 0});
it--;
return (it->m * x + it->b);
}
int main(void)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &h[i]);
ll S = 0;
for (int i = 1; i <= n; i++)
scanf("%lld", &w[i]), S += 1ll*w[i];
dp[1] = -w[1];
add(-2*h[1], h[1]*h[1]-w[1]);
for (int i = 2; i <= n; i++)
{
ll val = query(h[i]);
dp[i] = h[i]*h[i] - w[i] + val;
add(-2*h[i], h[i]*h[i]+dp[i]);
}
printf("%lld\n", S+dp[n]);
}
Compilation message
building.cpp: In function 'int main()':
building.cpp:108:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
building.cpp:111:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &h[i]);
~~~~~^~~~~~~~~~~~~~~
building.cpp:115:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &w[i]), S += 1ll*w[i];
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
2808 KB |
Output is correct |
2 |
Correct |
110 ms |
2808 KB |
Output is correct |
3 |
Correct |
98 ms |
2808 KB |
Output is correct |
4 |
Correct |
97 ms |
2652 KB |
Output is correct |
5 |
Correct |
87 ms |
4344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
98 ms |
2808 KB |
Output is correct |
7 |
Correct |
110 ms |
2808 KB |
Output is correct |
8 |
Correct |
98 ms |
2808 KB |
Output is correct |
9 |
Correct |
97 ms |
2652 KB |
Output is correct |
10 |
Correct |
87 ms |
4344 KB |
Output is correct |
11 |
Correct |
106 ms |
2684 KB |
Output is correct |
12 |
Correct |
131 ms |
2808 KB |
Output is correct |
13 |
Correct |
72 ms |
2720 KB |
Output is correct |
14 |
Correct |
109 ms |
2680 KB |
Output is correct |
15 |
Correct |
119 ms |
10464 KB |
Output is correct |
16 |
Correct |
88 ms |
4184 KB |
Output is correct |
17 |
Correct |
23 ms |
2680 KB |
Output is correct |
18 |
Correct |
21 ms |
2680 KB |
Output is correct |