Output: The longest tower is length 6 and includes from top to bottom: (56, 90) (60,95) (65,100) (68,110) (70,150) (75,190)
struct Node { int height; int weight; Node(int h, int w): height(h), weight(w) {} }; bool comp(const Node &left, const Node &right) { return left.height < right.height; } int fun(vector<Node> &a) { int n = a.size(); sort(a.begin(), a.end(), comp); int buf[n]; buf[0] = 1; int result = 1; for (int i = 1; i < n; i++) { buf[i] = 1; for (int j = 0; j < i; j++) { if (a[i].height > a[j].height && a[i].weight > a[j].weight) { buf[i] = max(buf[i], buf[j]+1); } } result = max(result, buf[i]); } return result; }