83522024-01-14 23:21:26TuruTamasFestés (50 pont)cpp17Time limit exceeded 10/50702ms35676 KiB
#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<set<ll>> f(ll i) {
    vector<set<ll>> ret;
    for (ll l = 0; l <= i; l++) {
        for (ll r = i; r < N; r++) {
            set<ll> s;
            for (ll i = l; i <= r; i++)
                s.insert(i);
            ret.push_back(s);
        }
    }
    return ret;
}

vector<vector<set<ll>>> vvs;
vector<vvi> C;
vi R;
set<ll> s, festett;

ll g(set<ll>::iterator it, ll m) {
    if (it == s.end())
        return 0;
    if (festett.count(*it))
        return g(++it, m);
    
    ll r = LLONG_MAX;
    for (auto range : vvs[*it]) {
        ll first = *range.begin();
        ll last = *range.rbegin();
        set<ll> to_remove;
        for (ll val : range) {
            if (festett.insert(val).second) {
                to_remove.insert(val);
            }
        }
        auto nit = it;
        nit++;
        r = min(r, C[m][first][last] + g(nit, m));
        for (ll val : to_remove)
            festett.erase(val);
    }
    return r;
}

void call_g(ll i) {
    if (i == N) {
        ll ans_tmp = 0;
        for (ll m = 0; m < M; m++) {
            ans_tmp += g(s.begin(), m);
        }
        for (ll sor = 0; sor < N; sor++) {
            if (!s.count(sor))
                ans_tmp += R[sor];
        }
        ans = min(ans, ans_tmp);
    } else {
        call_g(i+1);
        s.insert(i);
        call_g(i+1);
        s.erase(i);
    }
}

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++) {
        vvs[n] = f(n);
    }

    call_g(0);
    cout << ans << "\n";
}
SubtaskSumTestVerdictTimeMemory
base10/50
1Accepted0/03ms2076 KiB
2Accepted0/04ms2180 KiB
3Time limit exceeded0/2667ms21332 KiB
4Accepted2/213ms2772 KiB
5Accepted3/3108ms3420 KiB
6Time limit exceeded0/2658ms5360 KiB
7Time limit exceeded0/2702ms34312 KiB
8Time limit exceeded0/2647ms34328 KiB
9Time limit exceeded0/2667ms34464 KiB
10Time limit exceeded0/2676ms34772 KiB
11Time limit exceeded0/2661ms34720 KiB
12Time limit exceeded0/2648ms32340 KiB
13Time limit exceeded0/2669ms32524 KiB
14Accepted2/2216ms33632 KiB
15Accepted3/3216ms33464 KiB
16Time limit exceeded0/3667ms22984 KiB
17Time limit exceeded0/2656ms23132 KiB
18Time limit exceeded0/3667ms23008 KiB
19Time limit exceeded0/2657ms32468 KiB
20Time limit exceeded0/2685ms34164 KiB
21Time limit exceeded0/2680ms35440 KiB
22Time limit exceeded0/2657ms35664 KiB
23Time limit exceeded0/2644ms35676 KiB
24Time limit exceeded0/2652ms35612 KiB
25Time limit exceeded0/2657ms35672 KiB