hdu Sum of number
Sum of number
Problem Description
Input
The input consists of multiple test cases.The first line of input contains an integer N (3 ≤ N ≤ 100), the number of cards, and M (10 ≤ M ≤ 300 000), the number that we must not exceed.
The following line contains numbers written on Mirko‘s cards: N distinct space-separated positive integers less than 100 000. There will always exist some three cards whose sum is not greater than M.
The following line contains numbers written on Mirko‘s cards: N distinct space-separated positive integers less than 100 000. There will always exist some three cards whose sum is not greater than M.
Output
The first and only line of output should contain the largest possible sum we can obtain.
Sample Input
5 21 5 6 7 8 9 10 500 93 181 245 214 315 36 185 138 216 295
Sample Output
21 497
Source
2012暑假集训
三个有两种情况:1 .连续三个例如 (5 6 7)2.间隔三个(579),又数据范围100,n*n*n=1000,故暴力
#include <stdio.h>
#include <algorithm>
using namespace std;
#define maxn 150
#define max 200000
int a[maxn];
int s[maxn*maxn*maxn];
bool cmp( int x,int y)
{
return x>y;
}
int main( )
{
int n,m;
int i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
int max1=0;
for( i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int t=0;
for( i=1;i<=n;i++)
{
for( j=1;j<=n;j++)
{
for( k=1;k<=n;k++)
{
if(i!=j&&j!=k&&i!=k)
s[t++]=a[i]+a[j]+a[k];
}
}
}
sort(s,s+t,cmp);
i=0;
while(s[i]>m) i++;
printf("%d\n",s[i]);
}
return 0;
}
链接:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=16309&pid=1001