#include <bits/stdc++.h>
using namespace std;
#define forw(i,a,b) for(ll i=a;i<=b;i++)
#define forb(i,a,b) for(ll i=a;i>=b;i--)
typedef long long int ll;
ll res,n,h[200005],w[200005],max1[200005],pre,dp[200005];
void sub1()
{
for (int i=2;i<=n;i++)
{
max1[i]=1e18;
res=0;
for (int j=i-1;j>=1;j--)
{
ll p=(h[i]-h[j])*(h[i]-h[j])+res+max1[j];
if (p<max1[i]) max1[i]=p;
res+=w[j];
}
}
cout<<max1[n];
}
ll sqr(ll a)
{
return a*a;
}
void sub2()
{
ll ans=1e18;
ans=(h[n]-h[1])*(h[n]-h[1])+pre-w[1]-w[n];
map<int, set<int> > save;
forw(i,2,n-1)
{
forw(j,-20,20)
if (save[j].size())
{
auto it =lower_bound(save[j].begin(),save[j].end(),(h[i]+h[1])/2);
ans=min(ans,sqr(h[1]-h[i])+sqr(h[i]-h[n])+pre-w[1]-w[i]-w[n]);
if (it!=save[j].end())
{
ans=min(ans,sqr(h[1]-(*it))+sqr((*it)-h[i])+sqr(h[n]-h[i])+pre-w[1]-w[i]-w[n]-j);
}
if (it!=save[j].begin())
{
--it;
ans=min(ans,sqr(h[1]-(*it))+sqr((*it)-h[i])+sqr(h[n]-h[i])+pre-w[1]-w[i]-w[n]-j);
}
}
save[w[i]].insert(h[i]);
}
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("test.INP","r",stdin);
// freopen("test.OUT","w",stdout);
scanf("%lld",&n);
for (int i=1;i<=n;i++)
scanf("%lld",&h[i]);
bool ok=true;
for (int i=1;i<=n;i++)
{
scanf("%lld",&w[i]);
pre+=w[i];
if (abs(w[i])>20) ok=false;
}
if (!ok)sub1();
else sub2();
//sub2();
return 0;
}
Compilation message
building.cpp: In function 'int main()':
building.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%lld",&n);
| ~~~~~^~~~~~~~~~~
building.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%lld",&h[i]);
| ~~~~~^~~~~~~~~~~~~~
building.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%lld",&w[i]);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3057 ms |
2876 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Execution timed out |
3057 ms |
2876 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |