Pog and Szh are playing games.There is a sequence with n numbers, Pog will choose a number A from the sequence. Szh will choose an another number named B from the rest in the sequence. Then the score will be (A+B) mod p
.They hope to get the largest score.And what is the largest score?
Input
Several groups of data (no more than 5 groups,n≥1000).
For each case:
The following line contains two integers,n(2≤n≤100000),p(1≤p≤231−1)。
The following line contains n integers ai(0≤ai≤231−1)
。
Output
For each case,output an integer means the largest score.
Sample Input
4 4
1 2 3 0
4 4
0 0 2 2
Sample Output
3
2
注意:特殊的二分
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
long long int n,p,x;
long long int a[100000];
int i,j;
long long int r,l,mid;
long long int max;
while(scanf("%lld %lld",&n,&p)!=EOF)
{
for(i=0; i<n; i++)
{
scanf("%lld",&a[i]);
//printf("%lld ",a[i]);
if(a[i]>p)
a[i]=a[i]%p;
}
sort(a,a+n);
max=(a[n-1]+a[n-2])%p;
l=0;
r=n-1;
// printf("\n%lld ",a[n-1]);
while(r-l!=0)
{
// mid=(l+r)/2;
x=(a[l]+a[r]);
if(x%p>max)
max=x%p;
//printf("\n%lld",max);
if(x>=p)
r--;
if(x<p)
l++;
}
printf("%lld\n",max);
}
return 0;
}