Maximum sum of a subsequence having difference between their indices equal to the difference between their values | C # program for the above approach ; Function to find the maximum sum of a subsequence having difference between indices equal to difference in their values ; Stores the maximum sum ; Stores the value for each A [ i ] - i ; Traverse the array ; Update the value in map ; Update the answer ; Finally , print the answer ; Driver code ; Given Input ; Function Call
using System . Collections . Generic ; using System ; class GFG { static void maximumSubsequenceSum ( int [ ] A , int N ) { int ans = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( A [ i ] - i ) ) mp [ A [ i ] - i ] += A [ i ] ; else mp [ A [ i ] - i ] = A [ i ] ; ans = Math . Max ( ans , mp [ A [ i ] - i ] ) ; } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 10 , 7 , 1 , 9 , 10 , 1 } ; int N = A . Length ; maximumSubsequenceSum ( A , N ) ; } }