BerryYoghurt's blog

By BerryYoghurt, history, 5 years ago, In English

Hello. I was solving 1399E1 - Weights Division (easy version), and my initial solution had a TLE on test case 5. I checked out the tutorial and even read the attached solution, but I could not for the life of me find the reason why it exceeds the time limit. I have submitted 6 versions, each one with a slight optimization over the previous, until I finally figured out that an overflow is causing an infinite loop.

Now the question: Here is a version of the code which I thought should work 127717319, and here is the version that actually worked 127717507. The edges matrix stores for each edge the weight at index 0 and the number of paths this edge contributes to at index 1. It overflowed when it was a matrix of ints, and worked when I made it long long (this is the only difference between the two versions). Could anybody figure out why it overflows if the weights do not exceed 10^6; that is an int overflow should NOT happen?

  • Vote: I like it
  • +10
  • Vote: I do not like it