-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ4-HelpAshwin.cpp
70 lines (55 loc) · 1.61 KB
/
Q4-HelpAshwin.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//https://www.hackerearth.com/practice/algorithms/dynamic-programming/2-dimensional/practice-problems/algorithm/help-ashwin/
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
for(int k=0;k<t;k++)
{
int n,m;
cin>>n>>m;
int arr[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
cin>>arr[i][j];
}
int sec=0, lar=0;
int dp[n][m];
for(int j=0;j<m;j++)
{
if(arr[0][j]>=lar)
{
sec=lar;
lar=arr[0][j];
}
dp[0][j]=arr[0][j];
}
for(int i=1;i<n;i++)
{
int temp_lar=0,temp_sec=0;
for(int j=0;j<m;j++)
{
if(dp[i-1][j]==lar)
dp[i][j]=arr[i][j]+sec;
else
dp[i][j]=arr[i][j]+lar;
if(dp[i][j]>=temp_lar)
{
temp_sec =temp_lar;
temp_lar =dp[i][j];
}
else
if(dp[i][j]>temp_sec)
temp_sec=dp[i][j];
}
lar=temp_lar;
sec=temp_sec;
}
cout<<lar<<endl;
}
return 0;
}