Peter should buy books whose prices are 40 and 40.
Peter should buy books whose prices are 4 and 6.
Peter should buy books whose prices are 4 and 6.
Peter should buy books whose prices are 100000 and 1000000.

Why do I get Wrong Answer, I have already compared my output to all the outputs in the board saving them on a text file and using the Diff command in terminal to verify my output and the board output. It was always the same

Peter should buy books whose prices are 278 and 721.
Peter should buy books whose prices are 184 and 609.
Peter should buy books whose prices are 72 and 209.
Peter should buy books whose prices are 191 and 384.
Peter should buy books whose prices are 287 and 592.
Peter should buy books whose prices are 120 and 755.
Peter should buy books whose prices are 127 and 346.
Peter should buy books whose prices are 63 and 354.
Peter should buy books whose prices are 23 and 23.
Peter should buy books whose prices are 234 and 505.
Peter should buy books whose prices are 395 and 489.
Peter should buy books whose prices are 243 and 530.
Peter should buy books whose prices are 30 and 950.
Peter should buy books whose prices are 30 and 111.
Peter should buy books whose prices are 410 and 475.
Peter should buy books whose prices are 70 and 206.
Peter should buy books whose prices are 246 and 407.
Peter should buy books whose prices are 298 and 317.
Peter should buy books whose prices are 5 and 80.
Peter should buy books whose prices are 3 and 26.
Peter should buy books whose prices are 99 and 143.
Peter should buy books whose prices are 226 and 348.
Peter should buy books whose prices are 4 and 5.
Peter should buy books whose prices are 1 and 90.
Peter should buy books whose prices are 22 and 38.
Peter should buy books whose prices are 103 and 219.
Peter should buy books whose prices are 151 and 664.
Peter should buy books whose prices are 23 and 58.
Peter should buy books whose prices are 315 and 335.
Peter should buy books whose prices are 12 and 126.
Peter should buy books whose prices are 62 and 644.
Peter should buy books whose prices are 266 and 553.
Peter should buy books whose prices are 169 and 244.
Peter should buy books whose prices are 34 and 250.
Peter should buy books whose prices are 134 and 223.
Peter should buy books whose prices are 12 and 591.
Peter should buy books whose prices are 156 and 252.
Peter should buy books whose prices are 69 and 758.
Peter should buy books whose prices are 232 and 429.
Peter should buy books whose prices are 15 and 57.
Peter should buy books whose prices are 71 and 118.
Peter should buy books whose prices are 146 and 346.
Peter should buy books whose prices are 71 and 190.
Peter should buy books whose prices are 163 and 394.
Peter should buy books whose prices are 19 and 490.
Peter should buy books whose prices are 23 and 857.
Peter should buy books whose prices are 433 and 480.
Peter should buy books whose prices are 302 and 305.
Peter should buy books whose prices are 39 and 760.
Peter should buy books whose prices are 374 and 492.
Peter should buy books whose prices are 27 and 547.
Peter should buy books whose prices are 25 and 45.
Peter should buy books whose prices are 5 and 9.
Peter should buy books whose prices are 20 and 853.
Peter should buy books whose prices are 102 and 479.
Peter should buy books whose prices are 67 and 93.
Peter should buy books whose prices are 5 and 6.
Peter should buy books whose prices are 296 and 480.
Peter should buy books whose prices are 92 and 838.
Peter should buy books whose prices are 63 and 375.
Peter should buy books whose prices are 253 and 492.
Peter should buy books whose prices are 142 and 745.
Peter should buy books whose prices are 12 and 491.
Peter should buy books whose prices are 116 and 668.
Peter should buy books whose prices are 10 and 832.
Peter should buy books whose prices are 275 and 581.
Peter should buy books whose prices are 299 and 305.
Peter should buy books whose prices are 103 and 538.
Peter should buy books whose prices are 276 and 457.
Peter should buy books whose prices are 40 and 87.
Peter should buy books whose prices are 16 and 425.
Peter should buy books whose prices are 97 and 689.
Peter should buy books whose prices are 129 and 216.
Peter should buy books whose prices are 25 and 41.
Peter should buy books whose prices are 42 and 89.
Peter should buy books whose prices are 12 and 270.
Peter should buy books whose prices are 31 and 35.
Peter should buy books whose prices are 100 and 118.
Peter should buy books whose prices are 115 and 821.
Peter should buy books whose prices are 40 and 49.
Peter should buy books whose prices are 147 and 366.
Peter should buy books whose prices are 16 and 334.
Peter should buy books whose prices are 106 and 142.
Peter should buy books whose prices are 110 and 118.
Peter should buy books whose prices are 78 and 467.
Peter should buy books whose prices are 49 and 110.
Peter should buy books whose prices are 13 and 475.
Peter should buy books whose prices are 177 and 688.
Peter should buy books whose prices are 238 and 264.
Peter should buy books whose prices are 152 and 225.
Peter should buy books whose prices are 319 and 395.
Peter should buy books whose prices are 102 and 759.
Peter should buy books whose prices are 194 and 375.
Peter should buy books whose prices are 157 and 181.
Peter should buy books whose prices are 252 and 515.
Peter should buy books whose prices are 80 and 104.
Peter should buy books whose prices are 191 and 630.
Peter should buy books whose prices are 22 and 138.
Peter should buy books whose prices are 125 and 180.
Peter should buy books whose prices are 24 and 138.

Check input and AC output for thousands of problems on uDebug!

It seems that judges input are weak. I changed two lines in your code and got accepted but it was wrong solution.
With current judge's input - solutions with O(N * N) is accepted. My solution is in O(N * logN)

A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman