After I got the idea of this problem and solved it true, it's my first time to solve E during contest but sadly I got TLE it seems that the time limit is too strict.
I used loop in My Ternary search (my loop limit is 400 ) and It got TLE ,after the contest I changed it to 398 and it suddenly passed.
I asked about that in a comment and ho-jo-bo-ro-lo told me he use 500 in loop and it passed but I have to use cin.tie(0);
what about submitting the same code twice with cin.tie(0) one got TLE and the other got Accepted.
Time limit exceeded on test 22
during the contest I got TLE on Testcase #15 so if the TLE because reading and writing speed I think testcase 13,14 has the maximum limit and similar numbers so why it passed them and failed in 15.