As we all know, Xiao Cong is good at calculations, especially good at calculating whether one number is a multiple of another.
But Scallion is only good at two numbers. When there are a lot of numbers, it will be more distressed.
Now Scallion has given you n numbers. I hope you can find three numbers from these n numbers so that the sum of these three numbers is a multiple of K, and this sum is the largest.
The data guarantee must have a solution.
The first line of the input format
includes 2 positive integers n, K.
The n positive integers in the second line represent the given n numbers.
The output format
outputs an integer on a line to represent the sum.
The data range is
1≤n≤10^5,
1≤K≤10^3, and
the given n numbers do not exceed 10^8.
Input example:
4 3
1 2 3 4
Output example:
9
code show as below:
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int f[4][N];
vector<int> a[N];
int main()
{
int n,m;
cin>>n>>m;
for (int i = 0;i<n;i++)
{
int x;
cin>>x;
a[x%m].push_back(x);
}
memset(f,-0x3f,sizeof(f));
f[0][0] = 0;
for (int i = 0;i<m;i++)
{
sort(a[i].begin(),a[i].end());
reverse(a[i].begin(),a[i].end());
for (int u = 0;u<3 &&u <a[i].size();u++)
{
int t = a[i][u];
for (int j = 3;j>=1;j--)
for (int k = 0;k<m;k++)
{
f[j][k] = max(f[j][k],f[j-1][((k-t)%m+m)%m]+t);
}
}
}
cout<<f[3][0]<<endl;
return 0;
}