84492024-01-16 15:17:07TuruTamasFestés (50 pont)cpp17Time limit exceeded 22/50702ms42676 KiB
// a kurva anyját ennek a feladatnak, baszódjon meg!

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

#ifdef DEBUG
ifstream in_file("minta/be2.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<vvi> C;
vi R;
vector<ll> s;
vector<ll> festett;

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][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++) {
            ans_tmp += g(s.begin(), m);
            if (ans_tmp >= ans)
                return;
        }
        ans = min(ans, ans_tmp);
    } else {
        s.push_back(i);
        call_g(i+1);
        s.pop_back();
        call_g(i+1);
    }
}

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

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

    C.resize(M);
    for (ll m = 0; m < M; m++) {
        C[m].resize(N);
        for (ll l = 0; l < N; l++) {
            C[m][l].resize(N);
            for (ll r = l; r < N; r++) {
                input >> C[m][l][r];
            }
        }
    }

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

    call_g(0);
    cout << ans << "\n";
}
SubtaskSumTestVerdictTimeMemory
base22/50
1Accepted0/03ms1828 KiB
2Accepted0/03ms2024 KiB
3Accepted2/2310ms41148 KiB
4Accepted2/24ms2480 KiB
5Accepted3/314ms2976 KiB
6Accepted2/2104ms8696 KiB
7Time limit exceeded0/2671ms33988 KiB
8Time limit exceeded0/2637ms34276 KiB
9Time limit exceeded0/2675ms34516 KiB
10Time limit exceeded0/2671ms34768 KiB
11Time limit exceeded0/2652ms34584 KiB
12Time limit exceeded0/2648ms32216 KiB
13Time limit exceeded0/2669ms32292 KiB
14Accepted2/292ms33264 KiB
15Accepted3/393ms33064 KiB
16Accepted3/3272ms42544 KiB
17Accepted2/2248ms42544 KiB
18Accepted3/3207ms42676 KiB
19Time limit exceeded0/2662ms32116 KiB
20Time limit exceeded0/2680ms34056 KiB
21Time limit exceeded0/2656ms35416 KiB
22Time limit exceeded0/2652ms35796 KiB
23Time limit exceeded0/2680ms35640 KiB
24Time limit exceeded0/2702ms35808 KiB
25Time limit exceeded0/2675ms35996 KiB