-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.cpp
More file actions
42 lines (34 loc) · 839 Bytes
/
algorithm.cpp
File metadata and controls
42 lines (34 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <limits>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
int n;
std::vector<int> a, b;
std::vector<std::vector<int>> dp;
void testcase() {
std::cin >> n;
a = std::vector<int> (n);
b = std::vector<int> (n);
dp = std::vector<std::vector<int>> (n + 1, std::vector<int>(n + 1, 100*1000));
for (int i = 0; i < n; i++)
std::cin >> a[i];
for (int i = 0; i < n; i++)
std::cin >> b[i];
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = (a[i - 1] - 1) * (b[j - 1] - 1) + std::min(dp[i - 1][j], std::min(dp[i][j - 1], dp[i - 1][j - 1]));
}
}
std::cout << dp[n][n] << std::endl;
}
int main() {
std::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
for (int i = 0; i < t; ++i)
testcase();
}