// C++ program to find the Longest
// Increasing Subsequence having sum
// value atmost K
#include <bits/stdc++.h>
using namespace std;
int solve(int arr[], int N,
int prevele, int i, int K)
{
// check for base cases
if (i >= N || K <= 0)
return 0;
// check if it is possible to take
// current elements
if (arr[i] <= prevele
|| (K - arr[i] < 0)) {
return solve(arr, N, prevele,
i + 1, K);
}
// if current element is ignored
else {
int ans = max(
solve(arr, N, arr[i],
i + 1, K - arr[i])
+ 1,
solve(arr, N, prevele,
i + 1, K));
return ans;
}
}
// Driver Code
int main()
{
int N = 16;
int arr[N]
= { 0, 8, 4, 12,
2, 10, 6, 14,
1, 9, 5, 13,
3, 11, 7, 15 };
int K = 40;
cout << solve(arr, N,
INT_MIN, 0, K)
<< endl;
}