Log inSign up
Problems / B101

Booth carpets

200 ptsCases 15Set by dongbinna
Added October 30.
1 solved1 attempted5 submissionssolvers took 5.0 tries on average

Statement

The hallway of an exhibition hall has N booths standing in a single row, numbered 1 through N. The setup team has to lay a carpet on the floor of each booth. Carpets come in three colors, sand, moss and sea, and one booth gets only one color.

When the same color lies side by side, visitors find it hard to make out where one booth ends and the next begins. So two booths whose numbers differ by 1 have to get carpets of different colors. Booths whose numbers differ by 2 or more may get the same color.

The cost of laying a carpet differs by color and differs by booth as well. The setup team wants to carpet every booth while keeping the total cost as low as it can be.

Given the cost of laying each of the three colors of carpet in each booth, write a program that calculates the smallest total cost.

For example, suppose N=3. Let the cost of each color in each booth be as in the table below.

Booth123
Sand968972
Moss513755
Sea24873

Laying sea in booth 1, moss in booth 2 and sand in booth 3 makes every pair of touching booths a different color. The costs then work out as in the table below.

Booth123Total
Color pickedSeaMossSand
Cost23772111

Picking only the cheapest color in each booth gives sea for booth 1, moss for booth 2 and moss for booth 3, which makes booths 2 and 3 the same color, so it does not meet the condition. The answer cannot be found by picking a cheap color booth by booth like this, and the way the color picked in one booth ties the choice in the next booth has to be looked at alongside it. There is no way of keeping the condition that costs less than 111, so the answer in this example is 111.

Booth 1 and booth 3 have numbers that differ by 2, so they may get the same color. Picking sea, moss and sea, for example, gives 2 + 37 + 73 = 112, which also meets the condition but costs more than 111. When there is only 1 booth no booth touches it, so the cheapest of the three colors is picked.

Input and Output Examples

Example 1

Input

3
96 51 2
89 37 48
72 55 73

Output

111

Example 2

Input

5
68 62 38
34 21 22
12 52 81
13 74 42
14 31 93

Output

127

Example 3

Input

1
30 77 78

Output

30

Input

  • First the number of booths N is given on the first line. N is a natural number from 1 to 1,000.

  • Then N lines follow, giving the cost information for booth 1 through booth N in order. One line holds the cost of laying the sand, moss and sea carpet in that booth, in that order, separated by spaces. Each cost is a natural number from 1 to 1,000.

Output

  • Print on one line the smallest total cost of laying carpet in every booth. The cost is the costs of the carpets laid in the N booths all added together. Which color was picked is not printed.

Requirements

  • Two booths whose numbers differ by 1 get carpets of different colors
  • Booths whose numbers differ by 2 or more may get the same color
  • Every booth gets a carpet. No booth is left bare
  • The costs on a line are given in the order sand, moss, sea
  • You must write a program that reads from standard input and writes to standard output
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
  • There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 15 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
1
30 77 78
30
3
96 51 2
89 37 48
72 55 73
111
5
68 62 38
34 21 22
12 52 81
13 74 42
14 31 93
127

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 4.0ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 3 public cases. Submitting runs the hidden cases too.

People who solved this problem

The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.

1bampal243ms250 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Log in and you can see the code once you solve this problem.