Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n;
- int mat[105][105];
- int dp[105][105];
- int min_sum(int i, int j) {
- if(i == 0 and j == 0) {
- return mat[i][j];
- }
- if(dp[i][j] != -1) {
- return dp[i][j];
- }
- int result = 1e9;
- if(i - 1 >= 0) {
- result = min(result, min_sum(i - 1, j) + mat[i][j]);
- if(j - 1 >= 0) {
- result = min(result, min_sum(i - 1, j - 1) + mat[i][j]);
- }
- }
- if(j - 1 >= 0) {
- result = min(result, min_sum(i, j - 1) + mat[i][j]);
- }
- return dp[i][j] = result;
- }
- int main() {
- cin >> n;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < n; j++) {
- mat[i][j] = -1;
- dp[i][j] = -1;
- }
- }
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < n - i; j++) {
- cin >> mat[i][j];
- }
- }
- int result = 1e9;
- int ri, rj;
- for(int i = 0; i < n; i++) {
- if(result > min_sum(i, n - i - 1)) {
- result = min_sum(i, n - i - 1);
- ri = i;
- rj = n - i - 1;
- }
- }
- cout << result << endl;
- cout << ri + 1 << " " << rj + 1 << endl;
- return 0;
- }
- // f(0, 0) = f(1, 0) f(0, 1)
- // f(1, 0) = f(2, 0) f(1, 1)
- // f(0, 1) = f(1, 1) f(2, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement