Maximize the happiness of the groups on the Trip | C # implementation of the approach ; Function to return the maximized happiness ; Two arrays similar to 0 1 knapsack problem ; To store the happiness of the current goup ; Current person is a child ; Woman ; Man ; Old Person ; Group 's happiness is the sum of happiness  of the people in the group multiplie  the number of people ; Solution using 0 1 knapsack ; Driver code ; Number of seats ; Groups
using System ; class GFG { static int maxHappiness ( int A , int N , String [ ] v ) { String str ; int [ ] val = new int [ N ] ; int [ ] wt = new int [ N ] ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { str = v [ i ] ; c = 0 ; for ( int j = 0 ; j < str . Length ; j ++ ) { if ( str [ j ] == ' c ' ) c += 4 ; else if ( str [ j ] == ' w ' ) c += 3 ; else if ( str [ j ] == ' m ' ) c += 2 ; else c ++ ; } c *= str . Length ; val [ i ] = c ; wt [ i ] = str . Length ; } int [ , ] k = new int [ N + 1 , A + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int w = 0 ; w <= A ; w ++ ) { if ( i == 0 w == 0 ) k [ i , w ] = 0 ; else if ( wt [ i - 1 ] <= w ) { k [ i , w ] = Math . Max ( val [ i - 1 ] + k [ i - 1 , w - wt [ i - 1 ] ] , k [ i - 1 , w ] ) ; } else { k [ i , w ] = k [ i - 1 , w ] ; } } } return k [ N , A ] ; } public static void Main ( ) { int A = 5 ; String [ ] v = { " mmo " , " oo " , " cmw " , " cc " , " c " } ; int N = v . Length ; Console . WriteLine ( maxHappiness ( A , N , v ) ) ; } }