This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n, m;
void solve_n_1() {
vector<ll> a(n + m - 1, 0);
vector<ll> b(n + m - 1, 0);
for(ll &x : a) cin >> x;
for(ll &x : b) cin >> x;
reverse(b.begin(), b.end());
ll ans = 0;
for(int i = 0; i < n + m - 1; ++i) ans += min(a[i], b[i]);
cout << ans << "\n";
}
void solve_bf_1010() {
int p = n + m - 1;
vector<ll> a(p, 0);
vector<ll> b(p, 0);
for(ll &x : a) cin >> x;
for(ll &x : b) cin >> x;
vector<vector<bool> > colored(1 + n, vector<bool> (1 + m, false));
vector<bool> chosen(p, false);
vector<bool> paid(p, false);
ll ans = INT_MAX, cur = 0;
for(int mask = 0; mask < (1 << p); ++mask) {
cur = 0;
for(int i = 0; i < p; ++i) {
if(mask & (1 << i)) {
chosen[i] = true;
cur += a[i];
} else {
chosen[i] = false;
}
}
for(int i = 0; i < p; ++i) {
if(chosen[i]) {
int diff = m - 1 - i;
int y = m - i;
int x = y - diff;
if(y <= 0) {
int z = 1 - y;
x += z;
y += z;
}
while(x <= n && y <= m) {
colored[x][y] = true;
++x;
++y;
}
}
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
if(!colored[i][j]) {
paid[i + j - 2] = true;
}
}
}
for(int i = 0; i < p; ++i) {
if(paid[i]) {
cur += b[i];
}
}
ans = min(ans, cur);
chosen.assign(p, false);
paid.assign(p, false);
colored.assign(1 + n, vector<bool> (1 + m, false));
}
cout << ans << "\n";
}
void solve() {
cin >> n >> m;
if(n == 1) {
solve_n_1();
return;
}
if(n <= 20 && m <= 20) {
solve_bf_1010();
return;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |