Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n, m, a[50][50], dp[50][50] = {};
- cin >> n >> m;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- cin >> a[i][j];
- dp[0][0] = a[0][0];
- for (int i = 1; i < n; i++)
- dp[0][i] = a[0][i] + dp[0][i-1];
- for (int i = 1; i < m; i++)
- dp[i][0] = a[i][0] + dp[i-1][0];
- for (int i = 1; i < n; i++)
- for (int j = 1; j < m; j++)
- dp[i][j] = a[i][j] + max(dp[i-1][j], dp[i][j-1]);
- cout << dp[n-1][m-1] << endl;
- vector<pair<int, int> > result;
- int i = m - 1, j = n - 1;
- result.push_back({i+1, j+1});
- while (i != 0 || j != 0) {
- if (i > 0 && dp[i][j] == dp[i-1][j] + a[i][j]) {
- i--;
- } else {
- j--;
- }
- result.push_back({i+1, j+1});
- }
- reverse(result.begin(), result.end());
- for(int i=0; i<result.size(); i++){
- cout << result[i].first << " " << result[i].second << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement