For all those who are getting partial points,
try these case:
EDIT 1: 4 2 2 5 4 5 2 5 5 7 1 ans=19 4 2 2 5 5 7 1 5 4 5 2 ans=19 4 2 2 5 4 2 2 5 3 3 1 ans=14 4 2 2 5 3 3 1 5 4 2 2 ans=14 2 1 1 5 6 5 7 ans=12 2 1 1 5 7 5 6 ans=12 2 1 1 5 6 6 8 ans=13 4 1 3 7 5 2 2 6 2 9 8 EDIT 2: ans=28 3 2 1 7 4 9 7 2 3 ans=20 EDIT 3: 8 6 8 15 50 77 52 27 62 63 61 350 271 916 438 281 998 125 241 ans=3620 9 5 9 60 98 21 52 9 15 62 74 83 552 893 44 920 52 830 155 40 557 ans=4077
EDIT 4:
More test Cases with correct Output: http://ideone.com/rOMkjI