Comments

I think I would have TLE on my own problem D solution with test:

100000 10

1 2 ... 100000

1 2 ... 100000

...

1 2 ... 100000

100000 99999 ... 1