82562024-01-13 20:41:12szilFestés (50 pont)cpp17Accepted 50/50209ms43540 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 200'001;

ll c[MAXN][5][5], r[5];

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, m; cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> r[i];
    for (int i = 1; i <= m; i++) {
        for (int l = 1; l <= n; l++) {
            for (int r = l; r <= n; r++) {
                cin >> c[i][l][r];
            }
        }
    }
    ll ans = 1e18;
    for (int bits = 0; bits < (1<<n); bits++) {
        ll cost = 0;
        for (int i = 1; i <= n; i++) {
            if (bits&(1<<(i-1))) cost += r[i];
        }
        for (int i = 1; i <= m; i++) {
            vector<ll> dp(5, 1e18);
            dp[0] = 0;
            for (int j = 1; j <= n; j++) {
                if (bits&(1<<(j-1))) {
                    dp[j] = dp[j-1];
                }
                ll best = 1e18;
                for (int l = j; l >= 1; l--) {
                    best = min(best, dp[l-1]);
                    dp[j] = min(dp[j], c[i][l][j] + best);
                }
            }
            cost += dp[n];
        }
        ans = min(ans, cost);
    }
    cout << ans << "\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1900 KiB
2Accepted0/03ms2116 KiB
3Accepted2/2119ms41444 KiB
4Accepted2/23ms2672 KiB
5Accepted3/34ms3068 KiB
6Accepted2/220ms6828 KiB
7Accepted2/2202ms42248 KiB
8Accepted2/2196ms42248 KiB
9Accepted2/2200ms42500 KiB
10Accepted2/2202ms42452 KiB
11Accepted2/2201ms42244 KiB
12Accepted2/2180ms39140 KiB
13Accepted2/2188ms39400 KiB
14Accepted2/261ms42916 KiB
15Accepted3/368ms43000 KiB
16Accepted3/3115ms43092 KiB
17Accepted2/2115ms43360 KiB
18Accepted3/3116ms43352 KiB
19Accepted2/2184ms39560 KiB
20Accepted2/2193ms41448 KiB
21Accepted2/2201ms42992 KiB
22Accepted2/2202ms43452 KiB
23Accepted2/2202ms43448 KiB
24Accepted2/2202ms43452 KiB
25Accepted2/2209ms43540 KiB