Maximum amount of money that can be collected by a player in a game of coins | CPP Program to implement the above approach ; Function to calculate the maximum amount collected by A ; Stores the money obtained by A ; Stores mid elements of odd sized rows ; Size of current row ; Increase money collected by A ; Add coins at even indices to the amount collected by A ; Print the amount ; Driver Code ; Function call to calculate the amount of coins collected by A
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find ( int N , vector < vector < int > > Arr ) { int amount = 0 ; vector < int > mid_odd ; for ( int i = 0 ; i < N ; i ++ ) { int siz = Arr [ i ] . size ( ) ; for ( int j = 0 ; j < siz / 2 ; j ++ ) amount = amount + Arr [ i ] [ j ] ; if ( siz % 2 == 1 ) mid_odd . push_back ( Arr [ i ] [ siz / 2 ] ) ; } sort ( mid_odd . begin ( ) , mid_odd . end ( ) ) ; for ( int i = 0 ; i < mid_odd . size ( ) ; i ++ ) if ( i % 2 == 0 ) amount = amount + mid_odd [ i ] ; cout << amount << endl ; } int main ( ) { int N = 2 ; vector < vector < int > > Arr { { 5 , 2 , 3 , 4 } , { 1 , 6 } } ; find ( N , Arr ) ; }