```
int main(){
int t;cin>>t;
while(t--){
int n;cin>>n;vector<vector>d(n,vector(3));
for(int i=0;i>h>>w>>l;
d[i][0]=h;
d[i][1]=w;
d[i][2]=l;
}
for(int i=0;i<n;i++)
sort(d[i].begin(),d[i].end());
sort(d.begin(),d.end());
int dp[n];
memset(dp,0,sizeof(dp));
int ans=INT32_MIN;
``````
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
if(d[j][0]<d[i][0] && d[j][1]<d[i][1] && d[j][2]<d[i][2])
dp[i]=max(dp[i],dp[j]);
}
dp[i]+=d[i][2];
ans=max(dp[i],ans);
}
cout<<ans<<'\n';
}
```

}

# What is the order of input i.e first 3 numbers are dimensions of 1 box?Then why my answer is wrong

Hey use this logic-

**1)** Generate all 3 rotations of all boxes. The size of rotation array becomes 3 times the size of the original array. For simplicity, we consider width as always smaller than or equal to depth.

**2)** Sort the above generated 3n boxes in decreasing order of base area.

**3)** After sorting the boxes, the problem is same as LIS with following optimal substructure property.

MSH(i) = Maximum possible Stack Height with box i at top of stack

MSH(i) = { Max ( MSH(j) ) + height(i) } where j < i and width(j) > width(i) and depth(j) > depth(i).

If there is no such j then MSH(i) = height(i)

**4)** To get overall maximum height, we return max(MSH(i)) where 0 < i < n

Hope it clears all your doubts.

I hope Iâ€™ve cleared your doubt. I ask you to please rate your experience here

Your feedback is very important. It helps us improve our platform and hence provide you

the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen

the doubt.