This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define ar array
const int N = 40;
int a[N], dp[N][2];
int l[N], r[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
l[i] = n;
if(a[i] == -1) l[i] = 0;
else if(i) l[i] = l[i-1] + 1;
}
for(int i=n-1;~i;i--){
r[i] = n;
if(a[i] == -1) r[i] = 0;
else if(i + 1 < n) r[i] = r[i+1] + 1;
}
int tot = 0;
for(int mask=0;mask < (1 << n);mask++){
memset(dp, 1, sizeof dp);
bool is = 1;
dp[0][0] = dp[0][1] = 0;
for(int i=0;i<n && is;i++){
if(i){
dp[i][0] = dp[i-1][0] + 1;
dp[i][1] = dp[i-1][1] + 1;
}
if(a[i] == -1){
dp[i][0] = dp[i][1];
continue;
}
if(!(mask >> i & 1)) continue;
if(dp[i][0] > 2 * i) is = 0;
dp[i][1] = dp[i][0];
dp[i][0] += min(l[i], r[i]) * 2;
}
if(!is){
continue;
}
int res = 0;
for(int i=0;i<n;i++){
if(~a[i] && (mask >> i & 1)) res++;
}
tot = max(tot, res);
}
tot *= -1;
for(int i=0;i<n;i++){
if(~a[i]) tot += a[i];
}
cout<<tot<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |