84442024-01-16 15:09:34TuruTamasFestés (50 pont)cpp17Runtime error 2/50667ms71944 KiB
// a kurva anyját ennek a feladatnak, baszódjon meg!

#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
ifstream in_file("minta/be1.txt");
#define input in_file
#define INTHENAMEOFGOD
#else
#define input cin
#define INTHENAMEOFGOD \
    ios::sync_with_stdio(0); \
    cin.tie(0); \
    cout.tie(0);
#endif
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef array<ll, 2> pii;

ll N, M, a, ans = LLONG_MAX;
vector<vector<vector<ll>>> vvs;
vector<pair<vvi, ll>> C;
vi R;
vector<ll> s;
vector<ll> festett;
vi Csum;

void f(ll i, vector<vector<ll>> &v) {
    vector<vector<ll>> ret;
    for (ll l = 0; l <= i; l++) {
        for (ll r = i; r < N; r++) {
            vector<ll> s;
            for (ll i = l; i <= r; i++)
                s.push_back(i);
            v.push_back(s);
        }
    }
}

ll g(vector<ll>::iterator it, ll m) {
    if (it == s.end())
        return 0;
    if (find(festett.begin(), festett.end(), *it) != festett.end())
        return g(++it, m);

    ll r = LLONG_MAX;
    for (auto range : vvs[*it]) {
        ll first = *range.begin();
        ll last = *range.rbegin();
        ll to_remove = festett.size();
        for (ll val : range) {
            festett.push_back(val);
        }
        to_remove = festett.size() - to_remove;
        auto nit = it;
        nit++;
        r = min(r, C[m].first[first][last] + g(nit, m));
        for (ll i = 0; i < to_remove; i++)
            festett.pop_back();
    }

    return r;
}

void call_g(ll i) {
    if (i == N) {
        ll ans_tmp = 0;
        for (ll sor = 0; sor < N; sor++) {
            if (find(s.begin(), s.end(), sor) == s.end())
                ans_tmp += R[sor];
        }
        for (ll m = 0; m < M; m++) {
            if (ans_tmp >= ans)
                return;
            ans_tmp += g(s.begin(), m);
        }
        ans = min(ans, ans_tmp);
    } else {
        call_g(i+1);
        s.push_back(i);
        call_g(i+1);
        s.pop_back();
    }
}

int main() {
    INTHENAMEOFGOD
    input >> N >> M;

    R.resize(N);
    for (ll &val : R)
        input >> val;

    C.resize(M);
    ll ind = 0;
    for (ll m = 0; m < M; m++) {
        C[m].first.resize(N);
        for (ll l = 0; l < N; l++) {
            C[m].first[l].resize(N);
            for (ll r = l; r < N; r++) {
                input >> C[m].first[l][r];
                C[m].second = ind++;
            }
        }
    }
    Csum.assign(C.size(), 0);
    for (ll m = 0; m < M; m++) {
        for (vi &v : C[m].first) {
            Csum[m] += accumulate(v.begin(), v.end(), 0LL);
        }
    }
    sort(C.begin(), C.end(), [](pair<vvi, ll> &lhs, pair<vvi, ll> &rhs) {
        return Csum[lhs.second] > Csum[rhs.second];
    });

    vvs.resize(N);
    for (ll n = 0; n < N; n++) {
        f(n, vvs[n]);
    }

    call_g(0);
    cout << ans << "\n";
}
SubtaskSumTestVerdictTimeMemory
base2/50
1Accepted0/03ms1928 KiB
2Accepted0/03ms2268 KiB
3Runtime error0/2104ms44776 KiB
4Accepted2/24ms2840 KiB
5Runtime error0/34ms3720 KiB
6Runtime error0/217ms9848 KiB
7Runtime error0/2155ms70756 KiB
8Runtime error0/2159ms70756 KiB
9Runtime error0/2156ms70852 KiB
10Runtime error0/2164ms70756 KiB
11Runtime error0/2162ms70688 KiB
12Runtime error0/2149ms65404 KiB
13Runtime error0/2158ms65264 KiB
14Runtime error0/271ms36584 KiB
15Time limit exceeded0/3667ms19336 KiB
16Runtime error0/3101ms45992 KiB
17Runtime error0/2107ms46052 KiB
18Runtime error0/3107ms45976 KiB
19Runtime error0/2150ms64532 KiB
20Runtime error0/2157ms67876 KiB
21Runtime error0/2164ms70788 KiB
22Runtime error0/2165ms71436 KiB
23Runtime error0/2165ms71592 KiB
24Runtime error0/2159ms71708 KiB
25Runtime error0/2172ms71944 KiB