I randomly generated this
40
383969506850130277
899579838476750914
437379853279780485
901735416393661436
96831400381864048
890363150254190867
276346053747246733
302529506607501142
712431590560870556
484718723671663493
245631474907588959
289812771425276549
411643820293413440
730204283306370811
499797288895177071
259730734788778244
36425817932615895
494180931258792627
651392124063309348
679927701404658509
989256038613819896
18033035526726970
961885098931578705
537595904381797658
789974864447523124
970196214695864115
221123183472067578
61528257582404880
946645652547281100
660186507078938909
263240444629574780
267348537122523343
589477412740799117
388034917309861570
467887767544789704
699963081674160144
144891872005806359
789169283234602265
573909088618848666
938071190761138364
Your code gives 755. Correct answer is 780.