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>
#include <iomanip>
#define ll long long
#define int long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define str string
#define pii pair<int,int>
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define mii map<int,int>
#define mll map<ll,ll>
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define yess cout<<"Yes\n";
#define noo cout<<"No\n";
using namespace std;
#define maxn 1111111
#define mod 1000000007
int ans=100000000;
int n,h;
int a[101];
int j=0;
void dfs(int p,int z,int an){
if(p>n){
ans=min(ans,an);
return;
}
dfs(p+1,z,an+abs(a[p]-z));
for(int i=1;i<=h;i++){
if(z-i>=0)dfs(p+1,(z-i),an+abs(a[p]-((z-i))));
if(z+i<=4)dfs(p+1,abs(z+i),an+abs(a[p]-(abs(z+i))));
}
}
void solve(){
cin>>n>>h;
int f=0;
for(int i=1;i<=n;i++){
cin>>a[i];
f+=a[i];
}
if(h==0){
int r=f/n-1;
int e=0;
int k=f/n;
int l=f/n+1;
int q=0,w=0;
for(int i=1;i<=n;i++){
q+=abs(k-a[i]);
w+=abs(l-a[i]);
e+=abs(r-a[i]);
}
cout<<min(q,min(w,e));
return;
}
for(int i=0;i<=4;i++){
dfs(2,i,abs(a[1]-i));
}
cout<<ans;
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//srand( time(0));
//freopen("sum.in", "r", stdin);
//freopen("sum.out", "w", stdout);
int tests=1;
//cin>>tests;
for(int i=1;i<=tests;i++){
//cout<<"Case "<<i<<": \n";
solve();
}
}
# | 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... |
# | 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... |