# Codeforces 880e. Maximum subsequence (half-fold search + double pointer)

Source: Internet
Author: User
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 45 2 4 1`
Outputcopy
`3`
Inputcopy
`3 20199 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  % 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)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.