E. Maximum subsequencetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
You are given an arrayAConsistingNIntegers, and additionally an integerM. You have to choose some sequence of indicesB1, bytes,B2, middle..., middle ,...,BK(1 digit ≤ DigitB1 worker <workerB2 rows <values... rows <valuesBKLimit ≤ limitN) In such a way that the value of is maximized. Chosen sequence can be empty.
Print the maximum possible value.
Input
The first line contains two integersNAndM(1 digit ≤ DigitNLimit ≤ 35, 1 limit ≤ limitMLimit ≤ limit 109 ).
The second line containsNIntegersA1,A2 ,...,AN(1 digit ≤ DigitAILimit ≤ limit 109 ).
Output
Print the maximum possible value.
Examplesinputcopy
4 4
5 2 4 1
Outputcopy
3
Inputcopy
3 20
199 41 299
Outputcopy
19
Note
In the first example you can choose a sequenceBSums = sums {1, limit 2}, so the sum is equal to 7 (and that's 3 after taking it modulo 4 ).
In the second example you can choose a sequenceBToken = token {3 }.
/* Perform a half-fold search and store the sum after the modulo operation. Double-pointer statistical answer */# include <bits/stdc ++. h> # define n 300000 using namespace STD; int A [n], p [N], Q [N]; int K, T, ANS, n, m, B, dep, flag; inline int max (int x, int y) {return x> Y? X: Y;} inline int read () {int x = 0, F = 1; char c = getchar (); while (C> '9' | C <'0') {If (C = '-') F =-1; C = getchar ();} while (C> = '0' & C <= '9') {x = x * 10 + C-'0'; C = getchar ();} return x * F;} void DFS (INT now, int sum) {If (now = dep) {If (! Flag) {P [++ K] = sum, P [++ K] = (sum + A [B]) % m; return ;} else {q [++ T] = sum, Q [++ T] = (sum + A [n]) % m; return ;}} DFS (now + 1, sum); DFS (now + 1, (sum + A [now]) % m);} int main () {n = read (), M = read (), B = n> 1; Dep = B; For (INT I = 1; I <= N; ++ I) A [I] = read (); if (n = 1) printf ("% d", a [1] % m), exit (0); flag = 0; DFS (1, 0); Dep = N; flag = 1; DFS (B + 1, 0); int L = 0, r = T; sort (p + 1, P + k + 1); sort (q + 1, q + t + 1); While (L <= k) {While (P [l] + Q [R]> = m) -- r; ans = max (ANS, P [l] + Q [R]), ++ L;} ans = max (ANS, P [k] + Q [T]-M ); printf ("% d", ANS); Return 0 ;}
Codeforces 880e. Maximum subsequence (half-fold search + double pointer)