Under construction

Still being built. Things may change

Hidden Hierarchy

Interview

Time limit1sMemory limit512 MB

Summary
Build a directory tree from file paths, and print the smallest set of directories (expanding or collapsing as needed) that covers every directory whose total size is at least t.
Level

Medium6 of 10

Topics
Tree, Hash map, DFS, Greedy
Solved
No attempts yet

Problem

You are building the user interface of a simple text based file explorer. One of your tasks is a navigation pane that displays the directory hierarchy. The filesystem consists of directories that hold files and other directories, and those directories again hold files and directories, so the directories form a hierarchical tree. The topmost directory of the hierarchy is the root directory. If directory dd directly holds directory ee, then dd is the parent directory of ee and ee is a subdirectory of dd. Every file has a size in bytes. The size of a directory is the total size of all files held directly or indirectly inside it.

Every file and every directory except the root has a name, a string that always starts with a lowercase letter and consists of lowercase letters and the "." (dot) character only. Items (files and directories) placed directly inside the same parent directory must have distinct names. Every item is described uniquely by its path, built with the following rules.

  • The path of the root directory is a single slash, "/".
  • The path of a directory dd is obtained by concatenating the directory names top to bottom along the hierarchy from the root directory down to dd, putting a "/" character in front of each name and one more "/" character at the end.
  • The path of a file ff is the path of its parent directory followed by the name of ff.

The hierarchy is displayed by printing the root directory. Printing a directory dd means writing one line of the form "mdm_d pdp_d sds_d", where pdp_d and sds_d are the path and the size of dd, and mdm_d is its expansion marker, explained below. If dd holds other directories, you must choose either to collapse it or to expand it. If you expand dd, you print all of its subdirectories in lexicographical order by name, using the same rules. If you collapse dd, you simply ignore its contents.

The expansion marker mdm_d is a single blank character when dd has no subdirectory at all, the "+" (plus) character when you collapse dd, and the "-" (minus) character when you expand dd.

Given a list of files and a threshold integer tt, display the directory hierarchy so that every directory of size at least tt is printed and the total number of printed directories is as small as possible. Assume there are no empty directories, so the whole hierarchy follows from the given file paths. The root directory is printed no matter what its size is. A directory of size at least tt only has to be printed, it does not have to be expanded.

Input

The first line contains an integer nn (1≤n≤10001 \le n \le 1000), the number of files. Each of the following nn lines contains the path ff and the size ss (1≤s≤1061 \le s \le 10^6) of a single file, separated by a space. Each path is at most 100 characters long and is a valid file path under the rules above. All paths are different.

The next line contains an integer tt (1≤t≤1091 \le t \le 10^9), the threshold directory size.

Output

Print the display of the hierarchy for the given threshold that uses the smallest number of printed directories, as described above. Each line holds the expansion marker, one space, the path, one space, and the size, in that order.

Examples3

  1. Example 1

    Input
    9
    /sys/kernel/notes 100
    /cerc/problems/a/testdata/in 1000000
    /cerc/problems/a/testdata/out 8
    /cerc/problems/a/luka.cc 500
    /cerc/problems/a/zuza.cc 5000
    /cerc/problems/b/testdata/in 15
    /cerc/problems/b/testdata/out 4
    /cerc/problems/b/kale.cc 100
    /cerc/documents/rules.pdf 4000
    10000
    
    Expected output
    - / 1009727
    - /cerc/ 1009627
      /cerc/documents/ 4000
    - /cerc/problems/ 1005627
    - /cerc/problems/a/ 1005508
      /cerc/problems/a/testdata/ 1000008
    + /cerc/problems/b/ 119
    + /sys/ 100
    
  2. Example 2

    Input
    8
    /b/test/in.a 100
    /b/test/in.b 1
    /c/test/in.a 100
    /c/test/in.b 1
    /c/test/pic/in.a.svg 10
    /c/test/pic/in.b.svg 10
    /a/test/in.a 99
    /a/test/in.b 1
    101
    
    Expected output
    - / 322
    + /a/ 100
    - /b/ 101
      /b/test/ 101
    - /c/ 121
    + /c/test/ 121
    
  3. Example 3

    Input
    2
    /a/a/a 100
    /b.txt 99
    200
    
    Expected output
    + / 199