93222024-02-20 13:38:19AblablablaDinamitcpp17Accepted 50/5016ms5480 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll INF = 4e15 + 7;


ll n, m, k;
vector<vector<ll>> magassagok;
vector<vector<vector<ll>>> dp;

ll megold(ll sor, ll oszlop, ll robbantas){
    //cout << "hivas: " << sor << " " << oszlop << " " << robbantas << "\n";
    if(sor < 0 || n <= sor || oszlop < 0 || n <= oszlop || robbantas < 0){
        return INF;
    }
    if(dp[sor][oszlop][robbantas] != INF){
        return dp[sor][oszlop][robbantas];
    }

    ll vissza = INF;
    int mag = magassagok[sor][oszlop];

    for(ll i = 0; i <= robbantas; i++){
        ll jobbra = megold(sor, oszlop + 1, robbantas - i);
        ll lefele = megold(sor + 1, oszlop, robbantas - i);

        vissza = min(vissza, min(jobbra, lefele) + mag);
        mag /= 2;
    }

    dp[sor][oszlop][robbantas] = vissza;
    return vissza;
}

ll leker(int sor, int oszlop, int robbantas){
    if(sor < 0 || n <= sor || oszlop < 0 || m <= oszlop || robbantas < 0 || k < robbantas){
        return INF;
    }

    return dp[sor][oszlop][robbantas];
}

int main(){
    cin >> n >> m >> k;

    magassagok.assign(n, vector<ll>(m, 0));
    dp.assign(n, vector<vector<ll>>(m, vector<ll>(k + 1, INF))); // sor, oszlop, robbantas

    for(ll robbantas = 0; robbantas < n; robbantas++){
        for(ll sor = 0; sor < m; sor++){
            cin >> magassagok[robbantas][sor];
        }
    }

    dp[n - 1][m - 1][0] = magassagok[n - 1][m - 1];
    for(ll robbantas = 1; robbantas <= k; robbantas++){
        dp[n - 1][m - 1][robbantas] = dp[n - 1][m - 1][robbantas - 1] / 2;
    }

    for(int robbantas = 0; robbantas <= k; robbantas++){
        for(int sor = n - 1; sor >= 0; sor--){
            for(int oszlop = m - 1; oszlop >= 0; oszlop--){
                if(sor == n - 1 && oszlop == m - 1) continue;

                ll mag = magassagok[sor][oszlop];
                ll vissza = INF;

                for(ll i = 0; i <= robbantas; i++){
                    ll jobbra = leker(sor, oszlop + 1, robbantas - i);
                    ll lefele = leker(sor + 1, oszlop, robbantas - i);

                    vissza = min(vissza, min(jobbra, lefele) + mag);
                    mag /= 2;
                }

                dp[sor][oszlop][robbantas] = vissza;

                //cout << j << " " << l << " " << i << " : " << a << " " << b << " " << c << " " << d << " " << e << "\n";
            }
        }
    }

    cout << dp[0][0][k] << "\n";

    //cout << megold(0, 0, k) << "\n";

    /*for(ll i = 0; i <= k; i++){
        for(ll j = 0; j < n; j++){
            for(ll l = 0; l < m; l++){
                if(dp[j][l][i] == INF){
                    cout << "-1\t";
                    continue;
                }
                cout << dp[j][l][i] << "\t";
            }
            cout << "\n";
        }
        cout << "\n\n";
    }*/
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1816 KiB
2Accepted0/016ms3208 KiB
3Accepted2/23ms2464 KiB
4Accepted2/23ms2428 KiB
5Accepted3/34ms2704 KiB
6Accepted3/33ms2840 KiB
7Accepted2/214ms4036 KiB
8Accepted3/316ms4268 KiB
9Accepted2/23ms3424 KiB
10Accepted2/24ms3760 KiB
11Accepted3/33ms3980 KiB
12Accepted3/33ms3816 KiB
13Accepted2/26ms4056 KiB
14Accepted3/36ms4068 KiB
15Accepted2/216ms4860 KiB
16Accepted3/316ms5132 KiB
17Accepted2/216ms5240 KiB
18Accepted3/316ms5248 KiB
19Accepted2/216ms5480 KiB
20Accepted3/316ms5412 KiB
21Accepted2/216ms5452 KiB
22Accepted3/316ms5432 KiB