Cost of creating smallest subsequence with sum of difference between adjacent elements maximum |  ; initializing cost = 0 ; to store the removed element ; this will store the sum of the subsequence ; checking all the element of the vector ; storing the value of arr [ i ] in temp variable ; if the situation like arr [ i - 1 ] < arr [ i ] < arr [ i + 1 ] or arr [ i - 1 ] > arr [ i ] > arr [ i + 1 ] occur remove arr [ i ] i . e , temp from sequence ; insert the element in the set removedElements ; storing the value of arr [ i ] in temp ; taking the element not in removedElements ; adding the value of elements of subsequence ; if we have to remove the element then we need to add the cost associated with the element ; printing the sum of the subsequence with minimum length possible ; printing the cost incurred in creating subsequence ; Driver code ; calling the function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void costOfSubsequence ( int N , int arr [ ] , int costArray [ ] ) { int i , temp ; int cost = 0 ; set < int > removedElements ; int ans = 0 ; for ( i = 1 ; i < ( N - 1 ) ; i ++ ) { temp = arr [ i ] ; if ( ( ( arr [ i - 1 ] < temp ) && ( temp < arr [ i + 1 ] ) ) || ( ( arr [ i - 1 ] > temp ) && ( temp > arr [ i + 1 ] ) ) ) { removedElements . insert ( temp ) ; } } for ( i = 0 ; i < ( N ) ; i ++ ) { temp = arr [ i ] ; if ( ! ( removedElements . count ( temp ) > 0 ) ) { ans += arr [ i ] ; } else { cost += costArray [ i ] ; } } cout << ans << " , ▁ " ; cout << cost << endl ; } int main ( ) { int N ; N = 4 ; int arr [ N ] = { 1 , 3 , 4 , 2 } ; int costArray [ N ] = { 0 , 1 , 0 , 0 } ; costOfSubsequence ( N , arr , costArray ) ; return 0 ; }