Map Labeling

Place disjoint unit-height labels on a line above given points and count the minimum number of connectors that cannot run straight down to their own label.

Hard8Dynamic programmingSortingGreedyIntervalsNo attempts yetTime limit1sMemory limit512 MB

Problem

Map labeling places extra information, usually a text label, next to the features of interest on a map. A map typically shows line features (roads, rivers), area features (countries, forests, lakes), and point features (villages, cities). This problem considers point features only.

The basic requirements are that labels do not overlap each other and that each label lies close to the feature it belongs to. Large labels or a dense set of features make both requirements impossible to meet at once, so here a label may sit far from its feature as long as the labels stay pairwise disjoint. Every feature is joined to its label by a polygonal line called a connector, and connectors must not intersect each other. Only two kinds of connector exist. A straight connector is a single vertical segment. A bended connector is three connected segments: vertical, horizontal, then vertical again. See Figure G.1.

There is a line LL, taken as the x-axis, and nn points lie on it, one for each point feature. The coordinates of the nn points are distinct. A label is a rectangular area of height 1, so point pip_i at coordinate aia_i has an axis-parallel rectangular label lil_i of width wiw_i and height 1. All labels have the same height. Let UU be the line parallel to LL, above LL, at vertical distance 1 from LL. Each label rests with its lower side on UU and its body above UU, as in Figure G.1. Labels must be pairwise disjoint, but two labels may touch along their boundaries.

Figure G.1 Rectangular labels of point features

Write the position of lil_i as the interval [xi,xi+wi][x_i, x_i + w_i] on UU. A connector runs from its point to the lower side of its own label and stays inside the strip between LL and UU. The connector of pip_i is straight exactly when xiaixi+wix_i \le a_i \le x_i + w_i, because then the vertical segment from (ai,0)(a_i, 0) to (ai,1)(a_i, 1) reaches lil_i. Otherwise the connector is bended.

Write a program that finds a placement of the labels minimizing the number of bended connectors. For the points and labels of Figure G.1, the placement in Figure G.2 attains the minimum.

Figure G.2 Optimal placement of labels

Input

Your program reads from standard input. The first line has an integer nn (1n100001 \le n \le 10000), the number of points on LL. Each of the next nn lines has the coordinate aia_i of the ii-th point, an integer with 0ai1080 \le a_i \le 10^8. The coordinates are distinct, so aiaja_i \ne a_j when iji \ne j. Each of the following nn lines has the width wiw_i of the label of the ii-th point, an integer with 1wi1051 \le w_i \le 10^5. All labels have height 1.

Output

Your program writes to standard output. Print one line holding the minimum number of bended connectors over all valid placements.