189612025-11-13 11:58:43ubormaciDinamitcpp17Accepted 50/507ms1580 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
//#pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*
 *
 * notes:
 *
 * int64_t
 * stoi(string s) -> string to int
 * to_string() -> int (or else) to string
 *
 * vector declaration:
 * vector<ll> v(n, 0)
 * vector<vector<ll>> v(n, vector<ll>(n, 0));
 *
 * {if statement} ? {truth value} : {false value}
 *
 * set lower bound/upper bound:
 * 	// . . . m1 . . . d . . . . m2
 *    auto m1_it = b.lower_bound(d);
 *    advance(m1_it, -1);
 *    m1 = *m1_it;
 *	m2 = *b.upper_bound(d);
 *
 * #ifdef LOCAL
 *    freopen("in.txt","r",stdin);
 *    freopen("out.txt","w",stdout);
 * #endif
 *
 * constexpr auto lcm(auto x, auto... xs)
 * {
 *	return ((x = std::lcm(x, xs)), ...);
 * }
 *
 * std::gcd(int a, int b)
 *
 * cout << setprecision(n);
 *
 * set with custom ordering
 * set<ll, decltype(&cmp)> qu(cmp);
 *
 */

typedef int64_t ll;

const ll inf = INT64_MAX / 2;

void solve() {

    // n * m * k dp -> n,m,k <= 40 -> max komp 4^3 * 10^3 = 64 000 < 10^8
    // mennyi a minimalis ut az (i, j) mezore k dinamit felrobanntasaval?

    ll n, m, b;
    cin >> n >> m >> b;
    vector<vector<vector<ll>>> into(n, vector<vector<ll>>(m, vector<ll>(b+1, 0))); 
    // a leheto legkisebb kenyelmetlenseg k dinamit felhasznalasaval AHOGY ralepunk az (i,j)-re
    vector<vector<vector<ll>>> dp(n, vector<vector<ll>>(m, vector<ll>(b+1, 0)));
    // a leheto legkisebb kenyelmetlenseg k dinamit felhasznalasaval AMIKOR elhagyjuk az (i,j)-t

    vector<vector<ll>> v(n, vector<ll>(m, 0));
    for(ll i = 0; i < n; i++) {
        for(ll j = 0; j < m; j++) {
            cin >> v[i][j];
        }
    }

    for(ll i = 0; i < n; i++) {
        for(ll j = 0; j < m; j++) {
            for(ll k = 0; k <= b; k++) {

                ll intohere = 0;

                if(i == 0 && j == 0) {

                    intohere = 0;

                }
                else if(i == 0) {
                    intohere = dp[i][j-1][k];
                }
                else if(j == 0) {
                    intohere = dp[i-1][j][k];
                }else{
                    intohere = min(dp[i-1][j][k], dp[i][j-1][k]);
                }

                into[i][j][k] = intohere;
            }

            for(ll k = 0; k <= b; k++) {

                // tehat visszafele megyunk
                // dp[i][j][k]-nal vagy direkt az, ahogy igy bejovunk
                // vagy az ahogy k-1 bejovunk, plusz v[i][j] / (2^(kdiff))

                ll hw = v[i][j];

                dp[i][j][k] = inf;

                for(ll kk = k; kk  >= 0; kk--) {

                    dp[i][j][k] = min(dp[i][j][k], into[i][j][kk] + hw);
                    hw /= 2;
                }
            }
        }
    }

    cout << dp[n-1][m-1][b];

}

int main()
{
    std::ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);

    solve();

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/06ms1524 KiB
3Accepted2/21ms564 KiB
4Accepted2/22ms564 KiB
5Accepted3/31ms820 KiB
6Accepted3/32ms564 KiB
7Accepted2/26ms1384 KiB
8Accepted3/36ms1580 KiB
9Accepted2/21ms316 KiB
10Accepted2/21ms508 KiB
11Accepted3/31ms424 KiB
12Accepted3/31ms316 KiB
13Accepted2/22ms900 KiB
14Accepted3/33ms564 KiB
15Accepted2/26ms1580 KiB
16Accepted3/36ms1576 KiB
17Accepted2/27ms1332 KiB
18Accepted3/37ms1332 KiB
19Accepted2/26ms1580 KiB
20Accepted3/36ms1576 KiB
21Accepted2/26ms1576 KiB
22Accepted3/36ms1332 KiB