output_description: Print `delicious` if Takahashi found the food delicious; print `safe` if he
  neither found it delicious nor got a stomachache; print `dangerous` if he got
  a stomachache.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      X A B
attempt: # -*- coding: utf-8 -*-
  """
  Created on Thu Sep 20 21:40:30 2018
  ABC065D
  @author: maezawa
  i=0...n (0-origin)
  """
  import heapq
  
  n = int(input())
  a = []
  b = []
  for i in range(n):  # i is a name of the city (0-origin)
      xy = list(map(int, input().split()))
      a.append([i, xy[0]])
      b.append([i, xy[1]])
  
  a.sort(key=lambda x: x[1])
  b.sort(key=lambda x: x[1])
  # print(a)
  # print(b)
  
  # making adj list
  adj = [[] for _ in range(n)]
  for i, [j, x] in enumerate(a[:-1]):
      adj[j].append([a[i + 1][1] - x, a[i + 1][0]])
      adj[a[i + 1][0]].append([a[i + 1][1] - x, j])
  for i, [j, y] in enumerate(b[:-1]):
      adj[j].append([b[i + 1][1] - y, b[i + 1][0]])
      adj[b[i + 1][0]].append([b[i + 1][1] - y, j])
  
  # print(adj)
  
  # Prim's algorithm
  d = [10**10] * n
  visited = [False] * n
  h = []
  s = 0
  heapq.heappush(h, [0, s])
  cnt = 0
  while h:
      cost, node = heapq.heappop(h)
      # if visited[node] != True and cost < d[node]:
      if visited[node] != True:
          d[node] = cost
          visited[node] = True
          cnt += 1
          if cnt == n:
              break
          for edge in adj[node]:
              if visited[edge[1]] == True:
                  continue
              heapq.heappush(h, edge)
  
  # print(d)
  print(sum(d))
  
problem_description: Statement
  
  Takahashi has a strong stomach. He never gets a stomachache from eating
  something whose "best-by" date is at most X days earlier. He gets a
  stomachache if the "best-by" date of the food is X+1 or more days earlier,
  though.
  
  Other than that, he finds the food delicious if he eats it not later than the
  "best-by" date. Otherwise, he does not find it delicious.
  
  Takahashi bought some food A days before the "best-by" date, and ate it B days
  after he bought it.
  
  Write a program that outputs `delicious` if he found it delicious, `safe` if
  he did not found it delicious but did not get a stomachache either, and
  `dangerous` if he got a stomachache.
samples: [{"input": "4 3 6", "output": "safe\n    \n\nHe ate the food three days after the \"best-by\" date. It was not delicious or\nharmful for him.\n\n* * *"}, {"input": "6 5 1", "output": "delicious\n    \n\nHe ate the food by the \"best-by\" date. It was delicious for him.\n\n* * *"}, {"input": "3 7 12", "output": "dangerous\n    \n\nHe ate the food five days after the \"best-by\" date. It was harmful for him."}]