A
C
C
o
d
e
AC~Code
AC Code
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int n,k,maxn,w,ans,t,js;
int a[1010];
bool cmp(int x,int y)
{
return x%t>y%t;
}
int main()
{
freopen("berries.in","r",stdin);
freopen("berries.out","w",stdout);
cin>>n>>k;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
maxn=max(maxn,a[i]);
}
for(int i=1; i<=maxn; i++)
{
w=0;
for(int j=1; j<=n; j++)
w+=a[j]/i;
if(w<(k/2))
break;
if(w>=k)
{
ans=max(ans,i*(k/2));
continue;
}
t=i;
sort(a+1,a+n+1,cmp);
js=(w-k/2)*t;
for(int x=1; x<=n,x+w<=k; x++)
js+=a[x]%t;
ans=max(ans,js);
}
cout<<ans;
return 0;
}