Tower of Hanoi- HackerEarth- Dynamic Programming- Easy
Solution:-
using namespace std;
#define f(i,a,b) for(int i=a;i<b;i++)
long long arr[220];
vector<pair<long ,long> > tower;
bool my_comp(pair<long ,long> a,pair<long ,long> b)
{
return a.second>b.second;
}
int main()
{
int tc;
long long a,b,temp;
cin>>tc;
while(tc--)
{
int N;
cin>>N;
f(i,0,N)
{
cin>>a>>b;
tower.push_back(make_pair(a,b));
}
sort(tower.begin(),tower.end(),my_comp);
arr[0]=tower[0].second;
long long g_max=arr[0];
f(i,1,N)
{
long long max_val=-1;
f(j,0,i)
{
if(tower[i].first<tower[j].first && tower[i].second<tower[j].second)
temp=tower[i].second+arr[j];
else
temp=tower[i].second;
if(temp>max_val)
max_val=temp;
}
arr[i]=max_val;
if(arr[i]>g_max)
g_max=arr[i];
}
g_max=g_max>0?g_max:0;
cout<<g_max<<endl;
tower.clear();
}
return 0;
}
No comments:
Post a Comment