#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scan1(a) scanf("%lld",&a);
#define scan2(a,b) scanf("%lld %lld",&a, &b);
#define scan3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define endi puts("");
const int N = 1e6+12,INF=1e9+7;
int dp[101][7000][101],q[N],n;
int rec(int sum,int cnt,int j){
int i,f=0;
if (dp[j][sum][cnt])ret dp[j][sum][cnt];
for (i=1;i<=n;++i){
if (q[i] > 0 && q[i-1] > 0)f=1;
}
if (!f){
int x = 0;
for (i=1;i<=n;++i)x+=q[i];
sum+=x*3;
ret sum;
}
int ans = INF;
for (i=1;i<=n;++i){
if (q[i] <= 0)continue;
int mn = q[i];
q[i]-=mn;
ans = min(ans,rec(sum+mn*3,cnt+1,i));
q[i]+=mn;
if (i > 1&& q[i-1] > 0){
if (q[i-1] <= q[i]){
mn = q[i-1];
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(sum+mn*5,cnt+1,i));
q[i-1]+=mn;
q[i]+=mn;
}
else {
mn = q[i];
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(sum+mn*5,cnt+1,i));
q[i-1]+=mn;
q[i]+=mn;
}
}
if (i > 2 && q[i-2] > 0 && q[i-1] > 0){
if (q[i-1] <= q[i] && q[i-1] <= q[i-2]){
mn = q[i-1];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(sum+mn*7,cnt+1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
if (q[i-2] >= q[i] && q[i] <= q[i-1]){
mn = q[i];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(sum+mn*7,cnt+1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
if (q[i-1] <= q[i] && q[i-1] <= q[i-2]){
mn = q[i-1];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(sum+mn*7,cnt+1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
}
}
ret dp[j][sum][cnt] = ans;
}
main(){
int i,ans=INF;
scan1(n)
for (i=1;i<=n;++i){
scan1(q[i])
}
i=1;
int mn = q[i];
q[i]-=mn;
ans = min(ans,rec(mn*3,1,i));
q[i]+=mn;
i++;
if (i > 1){
if (q[i-1] <= q[i]){
mn = q[i-1];
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(mn*5,1,i));
q[i-1]+=mn;
q[i]+=mn;
}
else {
mn = q[i];
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(mn*5,1,i));
q[i-1]+=mn;
q[i]+=mn;
}
}
i++;
if (i > 2){
if (q[i-1] <= q[i] && q[i-1] <= q[i-2]){
mn = q[i-1];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(mn*7,1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
if (q[i-2] >= q[i] && q[i] <= q[i-1]){
mn = q[i];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(mn*7,1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
if (q[i-1] <= q[i] && q[i-1] <= q[i-2]){
mn = q[i-1];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(mn*7,1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
}
cout <<ans;
}
Compilation message
skyline.cpp:97:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
97 | main(){
| ^
skyline.cpp: In function 'int main()':
skyline.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
11 | #define scan1(a) scanf("%lld",&a);
| ~~~~~^~~~~~~~~~~
skyline.cpp:99:5: note: in expansion of macro 'scan1'
99 | scan1(n)
| ^~~~~
skyline.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
11 | #define scan1(a) scanf("%lld",&a);
| ~~~~~^~~~~~~~~~~
skyline.cpp:101:9: note: in expansion of macro 'scan1'
101 | scan1(q[i])
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Execution timed out |
2074 ms |
74188 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |