// Author: Ivan Kazmenko (gassa@mail.ru) module solution; // version = IO_FILES; import std.algorithm; import std.range; import std.stdio; import std.typecons; immutable string PROBLEM_NAME = "lights"; alias Lamp = Tuple !(int, q{x}, int, q{r}); bool check (Lamp [] lamps) { int limit = lamps.front.x; foreach (ref lamp; lamps) { if (lamp.x > limit) { return false; } limit = max (limit, lamp.x + lamp.r); } return true; } void main () { version (IO_FILES) { stdin = File (PROBLEM_NAME ~ ".in", "rt"); stdout = File (PROBLEM_NAME ~ ".out", "wt"); } int n; while (readf (" %s", &n) > 0) { auto lamps = new Lamp [n]; foreach (ref lamp; lamps) { readf (" %s %s", &lamp.x, &lamp.r); } auto lampsRev = lamps.retro .map !(lamp => Lamp (-lamp.x, lamp.r)).array; writeln (check (lamps) && check (lampsRev) ? (lamps.back.x - lamps.front.x) * 3L : -1); } }