fork download
  1. // Author: Ivan Kazmenko (gassa@mail.ru)
  2. module solution;
  3. // version = IO_FILES;
  4. import std.algorithm;
  5. import std.range;
  6. import std.stdio;
  7. import std.typecons;
  8.  
  9. immutable string PROBLEM_NAME = "lights";
  10.  
  11. alias Lamp = Tuple !(int, q{x}, int, q{r});
  12.  
  13. bool check (Lamp [] lamps)
  14. {
  15. int limit = lamps.front.x;
  16. foreach (ref lamp; lamps)
  17. {
  18. if (lamp.x > limit)
  19. {
  20. return false;
  21. }
  22. limit = max (limit, lamp.x + lamp.r);
  23. }
  24. return true;
  25. }
  26.  
  27. void main ()
  28. {
  29. version (IO_FILES)
  30. {
  31. stdin = File (PROBLEM_NAME ~ ".in", "rt");
  32. stdout = File (PROBLEM_NAME ~ ".out", "wt");
  33. }
  34.  
  35. int n;
  36. while (readf (" %s", &n) > 0)
  37. {
  38. auto lamps = new Lamp [n];
  39. foreach (ref lamp; lamps)
  40. {
  41. readf (" %s %s", &lamp.x, &lamp.r);
  42. }
  43. auto lampsRev = lamps.retro
  44. .map !(lamp => Lamp (-lamp.x, lamp.r)).array;
  45. writeln (check (lamps) && check (lampsRev) ?
  46. (lamps.back.x - lamps.front.x) * 3L : -1);
  47. }
  48. }
  49.  
Success #stdin #stdout 0s 17424KB
stdin
5
1 5
3 1
4 9
7 8
8 4
stdout
21