//
// main.cpp
// Subset with given sum
//
// Created by Himanshu on 18/09/21.
//
#include <iostream>
using namespace std;
const int N = 7;
int solve (int A[], int sum) {
int dp[sum+1][N+1];
for (int i=1; i<=sum; i++) {
for (int j=1; j<=N; j++) {
dp[i][j] = 0;
}
}
dp[0][0] = 1;
for (int i=1; i<=N; i++) {
dp[0][i] = 1;
}
for (int i=1; i<=sum; i++) {
dp[i][0] = 0;
}
for (int i=1; i<=sum; i++) {
for (int j=1; j<=N; j++) {
dp[i][j] = dp[i][j-1];
if (A[j-1] <= i) {
dp[i][j] = dp[i][j] + dp[i-A[j-1]][j-1];
}
}
}
return dp[sum][N];
}
int main() {
int A[N] = {5, 3, 4, 6, 8, 11, 20};
int sum = 15;
cout<<"Number of subsets with sum "<<sum<<": "<<solve(A, sum)<<endl;
sum = 57;
cout<<"Number of subsets with sum: "<<sum<<": "<<solve(A, sum)<<endl;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBTdWJzZXQgd2l0aCBnaXZlbiBzdW0KLy8KLy8gIENyZWF0ZWQgYnkgSGltYW5zaHUgb24gMTgvMDkvMjEuCi8vCgojaW5jbHVkZSA8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBOID0gNzsKCmludCBzb2x2ZSAoaW50IEFbXSwgaW50IHN1bSkgewogICAgaW50IGRwW3N1bSsxXVtOKzFdOwogICAgCiAgICBmb3IgKGludCBpPTE7IGk8PXN1bTsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaj0xOyBqPD1OOyBqKyspIHsKICAgICAgICAgICAgZHBbaV1bal0gPSAwOwogICAgICAgIH0KICAgIH0KICAgIAogICAgZHBbMF1bMF0gPSAxOwogICAgZm9yIChpbnQgaT0xOyBpPD1OOyBpKyspIHsKICAgICAgICBkcFswXVtpXSA9IDE7CiAgICB9CiAgICAKICAgIGZvciAoaW50IGk9MTsgaTw9c3VtOyBpKyspIHsKICAgICAgICBkcFtpXVswXSA9IDA7CiAgICB9CiAgICAKICAgIGZvciAoaW50IGk9MTsgaTw9c3VtOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqPTE7IGo8PU47IGorKykgewogICAgICAgICAgICBkcFtpXVtqXSA9IGRwW2ldW2otMV07CiAgICAgICAgICAgIGlmIChBW2otMV0gPD0gaSkgewogICAgICAgICAgICAgICAgZHBbaV1bal0gPSBkcFtpXVtqXSArIGRwW2ktQVtqLTFdXVtqLTFdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICByZXR1cm4gZHBbc3VtXVtOXTsKICAgIAp9CiAKaW50IG1haW4oKSB7CiAgICBpbnQgQVtOXSA9IHs1LCAzLCA0LCA2LCA4LCAxMSwgMjB9OwogICAgaW50IHN1bSA9IDE1OwogICAgCiAgICBjb3V0PDwiTnVtYmVyIG9mIHN1YnNldHMgd2l0aCBzdW0gIjw8c3VtPDwiOiAiPDxzb2x2ZShBLCBzdW0pPDxlbmRsOwogICAgCiAgICBzdW0gPSA1NzsKICAgIGNvdXQ8PCJOdW1iZXIgb2Ygc3Vic2V0cyB3aXRoIHN1bTogIjw8c3VtPDwiOiAiPDxzb2x2ZShBLCBzdW0pPDxlbmRsOwp9Cg==