Skip to content

Instantly share code, notes, and snippets.

@nachiketkanore
Created June 4, 2021 11:35
Show Gist options
  • Save nachiketkanore/9a2290798358dd57fbd26e64235971a5 to your computer and use it in GitHub Desktop.
Save nachiketkanore/9a2290798358dd57fbd26e64235971a5 to your computer and use it in GitHub Desktop.
int go(int L, int R){
if(L > R)
return 0;
if(L == R)
return a[L];
int &ans = dp[L][R];
if(ans != -1)
return ans;
int c1 = a[L] + go(L+1,R);
int c2 = a[L]*a[L+1] + go(L + 2, R);
int c3 = a[R] + go(L,R-1);
int c4 = a[R]*a[R-1] + go(L, R - 2);
ans = max({c1, c2, c3, c4});
return ans;
}
// final_ans = go(1, n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment