-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum_subsequence.cpp
executable file
·61 lines (47 loc) · 1.06 KB
/
maximum_subsequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
vector<int> X, Y;
void generate_subcjto(vector<int> L, vector<int> &X, int len, int ind, int M)
{
for(int i = 0; i < (1 << len); i++)
{
int sum = 0;
for(int j = 0; j < len; j++)
{
if(i & (1 << j))
sum = (sum + L[j + ind]) % M;
}
X.push_back(sum);
}
}
int get_solution(vector<int> L, int N, int M)
{
int len_X = N / 2;
int len_Y = N - len_X;
generate_subcjto(L, X, len_X, 0, M);
generate_subcjto(L, Y, len_Y, N / 2, M);
sort(Y.begin(), Y.end());
int resp = 0;
for(int i = 0; i < X.size(); i++)
{
int v = X[i];
int pos = upper_bound(Y.begin(), Y.end(), M - 1 - v) - Y.begin() - 1;
if(pos >= 0)
resp = max(resp, v + Y[pos]);
}
return resp;
}
int main()
{
int N, M, a;
cin >> N >> M;
vector<int> L;
for(int i = 0; i < N; i++)
{
cin >> a;
L.push_back(a);
}
int resp = get_solution(L, N, M);
cout << resp << endl;
}