Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.7 kB
3
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin() , v.end() 

int n , s , mid , ans = LLONG_MAX;
vector<int> v;
vector<int> st0 , st1 , st2;
vector<int> end0 , end1 , end2;

void rec1(int i , vector<int> temp , int sum) {
    if(temp.size() >= 3) {
        int sz = temp.size();
        if(sz >= 3 && temp[sz - 1] < 0 && temp[sz - 2] < 0 && temp[sz - 3] < 0) return;
    }
    if(i == mid) {
       int sz = temp.size();
       if(sz >= 2) {
            if(temp[sz - 1] < 0 && temp[sz - 2] < 0 ) end2.push_back(sum);
            else if(temp[sz - 1] < 0) end1.push_back(sum);
            else end0.push_back(sum);
        }
        else if(sz) {
            if(temp[sz - 1] < 0) end1.push_back(sum);
            else end0.push_back(sum);
        }
        if(sum >= s) ans = min(ans , sum);
        return;
    }
    
    temp.push_back(v[i]);
    rec1(i + 1 , temp , sum + v[i]);
    temp.pop_back();
    rec1(i + 1 , temp , sum);

}

void rec2(int i , vector<int> temp , int sum) {
    if(temp.size() >= 3) {
        int sz = temp.size();
        if(sz >= 3 && temp[sz - 1] < 0 && temp[sz - 2] < 0 && temp[sz - 3] < 0) return;
    }
    if(i == n) {
       int sz = temp.size();
        if(sz >= 2) {
            if(temp[0] < 0 && temp[1] < 0) st2.push_back(sum);
            else if(temp[0] < 0) st1.push_back(sum);
            else st0.push_back(sum);
        }
        else if(sz) {
            if(temp[0] < 0) st1.push_back(sum);
            else st0.push_back(sum);
        }
        if(sum >= s) ans = min(ans , sum);
        return;
    }
    
    temp.push_back(v[i]);
    rec2(i + 1 , temp , sum + v[i]);
    temp.pop_back();
    rec2(i + 1 , temp , sum);
    
}
void solve() {
    // meet in the middle
    cin >> n >> s;
    v = vector<int> (n);
    for(auto &i : v) cin >> i;
    
    mid = n / 2;
    rec1(0 , {} , 0);
    rec2(mid , {} , 0);
    
    sort(all(st0));
    sort(all(st1));
    sort(all(st2));

    for(int i = 0; i < end0.size(); ++i) {
        mid = s - end0[i];
        if(!st0.empty()) {
            auto it = lower_bound(all(st0) , mid) - st0.begin();
            if(it != st0.size()) ans = min(ans , end0[i] + st0[it]);// , cout << "a\n";
        }
        if(!st1.empty()) {
            auto it = lower_bound(all(st1) , mid) - st1.begin();
            if(it != st1.size()) ans = min(ans , end0[i] + st1[it]);// , cout << "b\n";
        }
        if(!st2.empty()) {
            auto it = lower_bound(all(st2) , mid) - st2.begin();
            if(it != st2.size()) ans = min(ans , end0[i] + st2[it]);// , cout << "c\n";
        }
    }

    for(int i = 0; i < end1.size(); ++i) {
        mid = s - end1[i];
        if(!st0.empty()) {
            auto it = lower_bound(all(st0) , mid) - st0.begin();
            if(it != st0.size()) ans = min(ans , end1[i] + st0[it]);// , cout << "d\n";
        }
        if(!st1.empty()) {
            auto it = lower_bound(all(st1) , mid) - st1.begin();
            if(it != st1.size()) ans = min(ans , end1[i] + st1[it]);// , cout << "e\n";
        }
    }

    for(int i = 0; i < end2.size() && !st0.empty(); ++i) {
        mid = s - end2[i];
        auto it = lower_bound(all(st0) , mid) - st0.begin();
        if(it != st0.size()) ans = min(ans , end2[i] + st0[it]);// , cout << "f\n";
    }
    
    if(ans == LLONG_MAX) cout << "Impossible\n";
    else cout << ans << '\n';
}

int32_t main() {
    std::ios::sync_with_stdio(false);
    ios_base::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while (t--)solve();
}
Leave a Comment