83552024-01-14 23:33:08TuruTamasFestés (50 pont)cpp17Time limit exceeded 22/50689ms43048 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<vector<ll>> f(ll i) {
    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);
            ret.push_back(s);
        }
    }
    return ret;
}

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

ll g(vector<ll>::iterator it, ll m) {
    if (it == s.end())
        return 0;
    if (count(festett.begin(), festett.end(), *it))
        return g(++it, m);
    
    ll r = LLONG_MAX;
    for (auto range : vvs[*it]) {
        ll first = *range.begin();
        ll last = *range.rbegin();
        ll to_remove = 0;
        for (ll val : range) {
            if (!count(festett.begin(), festett.end(), val)) {
                festett.push_back(val);
                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 m = 0; m < M; m++) {
            ans_tmp += g(s.begin(), m);
        }
        for (ll sor = 0; sor < N; sor++) {
            if (!count(s.begin(), s.end(), sor))
                ans_tmp += R[sor];
        }
        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);
    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
base22/50
1Accepted0/03ms1824 KiB
2Accepted0/03ms2024 KiB
3Accepted2/2375ms41132 KiB
4Accepted2/24ms2604 KiB
5Accepted3/323ms3476 KiB
6Accepted2/2209ms9280 KiB
7Time limit exceeded0/2670ms34652 KiB
8Time limit exceeded0/2652ms34860 KiB
9Time limit exceeded0/2675ms34820 KiB
10Time limit exceeded0/2672ms34968 KiB
11Time limit exceeded0/2672ms34812 KiB
12Time limit exceeded0/2680ms32440 KiB
13Time limit exceeded0/2656ms32192 KiB
14Accepted2/2105ms33268 KiB
15Accepted3/3107ms33228 KiB
16Accepted3/3372ms42712 KiB
17Accepted2/2365ms42988 KiB
18Accepted3/3365ms43048 KiB
19Time limit exceeded0/2660ms32568 KiB
20Time limit exceeded0/2675ms34184 KiB
21Time limit exceeded0/2680ms35568 KiB
22Time limit exceeded0/2663ms35856 KiB
23Time limit exceeded0/2684ms36044 KiB
24Time limit exceeded0/2663ms36044 KiB
25Time limit exceeded0/2689ms36036 KiB