fork(1) download
  1. def reader(inFile):
  2. (numStops, numPairs) = tuple(inFile.getInts())
  3. return (numStops,[tuple(inFile.getInts()) for k in xrange(numPairs)])
  4.  
  5. def f(N,k):
  6. return k * N - ((k * (k - 1)) / 2)
  7.  
  8. def solver((numStops, routes)):
  9. cost = sum([r[2] * f(numStops, r[1] - r[0]) for r in routes])
  10. minCost = 0
  11. events = [(r[i], i, r[2]) for r in routes for i in xrange(2)]
  12. events.sort()
  13. queue = []
  14. for event in events:
  15. if event[1] == 0:
  16. queue += [(event[0], event[2])]
  17. else:
  18. numToHandle = event[2]
  19. while (numToHandle > 0):
  20. num = min(queue[-1][1], numToHandle)
  21. minCost += num * f(numStops, event[0] - queue[-1][0])
  22. numToHandle -= num
  23. if num == queue[-1][1]:
  24. queue = queue[:-1]
  25. else:
  26. queue[-1] = (queue[-1][0], queue[-1][1] - num)
  27. return (cost - minCost) % 1000002013
  28.  
  29. if __name__ == "__main__":
  30. from GCJ import GCJ
  31. GCJ(reader, solver, "/Users/lpebody/gcj/2013_2/a/", "a").run()
  32.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty