#include <algorithm>
#include <bitset>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define int long long
#define loop(X, N) for(int X = 0; X < (N); X++)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef uint64_t u64;
typedef int64_t i64;
int h, w;
vi a, b;
int minIdx(const vi& arr, int l, int r) {
int best = l;
for (int i = l; i <= r; i++) {
if (arr[i] < arr[best])
best = i;
}
return best;
}
int solve(int i1, int j1, int i2, int j2) {
if (i1 == i2) {
return (j2 - j1) * a[i1];
}
if (j1 == j2) {
return (i2 - i1) * b[j1];
}
int bestI = minIdx(a, i1, i2);
int bestJ = minIdx(b, j1, j2);
if (bestI == i1 && bestJ == j1) {
return min(
solve(i1, j1, i1 + 1, j1) + solve(i1 + 1, j1, i2, j2),
solve(i1, j1, i1, j1 + 1) + solve(i1, j1 + 1, i2, j2)
);
}
else if (bestI == i2 && bestJ == j2) {
return min(
solve(i1, j1, i2 - 1, j2) + solve(i2 - 1, j2, i2, j2),
solve(i1, j1, i2, j2 - 1) + solve(i2, j2 - 1, i2, j2)
);
}
else {
return solve(i1, j1, bestI, bestJ) + solve(bestI, bestJ, i2, j2);
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> h >> w;
a = vi(h);
b = vi(w);
loop(i, h) cin >> a[i];
loop(i, w) cin >> b[i];
cout << solve(0, 0, h - 1, w - 1) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
7 ms |
472 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
2080 ms |
348 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Execution timed out |
2089 ms |
348 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
7 ms |
472 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
2080 ms |
348 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |