round vaults in Man's house

IN THE STRONG ROOM IN ONE MAN’S HOUSE.THERE ARE N VAULT IN A CIRCLE .THE AMOUNT OF MONEY INSIDE EACH VAULT
SHOW ON DISPLAY(WHICH IS FRONT OF VAULT’S DOOR).YOU CAN EMPTY ANY NUMBER OF VAULTS AS LONG AS YOU DO NOT EMPTY MORE THEN 2 OF ANY 5 ADJACENT VAULTS ,AN SECURITY SYSTEM SOUNDS AND SENTRY ALL DOOR ARE CLOSED AND AUTOMATIC LASER GUN SHOOT YOU! NOTE THAT AS THE VAULTS ARE ARRANGED IN CIRCLE ,THE LAST VAULT IS
ADJACENT TO FIRST ONE.

THE OUTPUT IS THE MAXIMUM AMOUNT OF MONEY THAT CAN BE EMPTIED WITHOUT SOUNDING THE SECURITY ALARM

CONSTRAINS N<=50 AMOUNT IN EACH VAULT <=50000 (N*1000)

EXAMPLE:

8

1000,2000,1000,5000,9000,5000,3000,4000.

OUTPUT :

15000

THE VAULTS 1,5,6 ARE LOOTED ,GIVING A TOTAL LOOT OF (1000+5000+9000)=15000

Click to view

hidden text

Click to view

hidden text

//