#include <iostream>
#include <istream>
#include <vector>
#include <algorithm>
using namespace std;

#define NV(v){cout<<#v<<" : "<<(v)<<'\n';}

vector<long long> gen(vector<int>& nums, long long p) {
    vector<long long> res;
    res.push_back(0);
    for (int num : nums) {
        int sz = res.size();
        for (int i = 0; i < sz; i++) {
            long long new_sum = (res[i] + num) % p;
            res.push_back(new_sum);
        }
    }
    return res;
}

int main() {
    iostream::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    long long p;
    cin >> n >> p;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    if (n == 0) {
        cout << 0 << endl;
        return 0;
    }

    int mid = n / 2;
    vector<int> A(a.begin(), a.begin() + mid);
    vector<int> B(a.begin() + mid, a.end());

    vector<long long> T1 = gen(A, p);
    vector<long long> T2 = gen(B, p);

    sort(T2.begin(), T2.end());
    auto last = unique(T2.begin(), T2.end());
    T2.erase(last, T2.end());

    long long ans = 0;
    for (long long x : T1) {
        long long target = p - 1 - x;
        auto it = upper_bound(T2.begin(), T2.end(), target);
        if (it != T2.begin()) {
            --it;
            ans = max(ans, x + *it);
        }

        if (!T2.empty()) {
            long long max_val = T2.back();
            if (max_val >= p - x) {
                ans = max(ans, (x + max_val) % p);
            }
        }
    }

    cout << ans << endl;
    return 0;
}