codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

431B.md (2851B)


      1 Source: https://codeforces.com/problemset/problem/431/B
      2 
      3 B. Shower Line
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks.
     22 
     23 There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower.
     24 
     25 Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2*i* - 1)-th man in the line (for the current moment) talks with the (2*i*)-th one.
     26 
     27 Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower.
     28 
     29 We know that if students *i* and *j* talk, then the *i*-th student's happiness increases by *g**ij* and the *j*-th student's happiness increases by *g**ji*. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower.
     30 
     31 Input
     32 
     33 The input consists of five lines, each line contains five space-separated integers: the *j*-th number in the *i*-th line shows *g**ij* (0 ≤ *g**ij* ≤ 105). It is guaranteed that *g**ii* = 0 for all *i*.
     34 
     35 Assume that the students are numbered from 1 to 5.
     36 
     37 Output
     38 
     39 Print a single integer — the maximum possible total happiness of the students.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 0 0 0 0 9  
     47 0 0 0 0 0  
     48 0 0 0 0 0  
     49 0 0 0 0 0  
     50 7 0 0 0 0
     51 ```
     52 
     53 Output
     54 
     55 ```
     56 32
     57 ```
     58 
     59 Input
     60 
     61 ```
     62 0 43 21 18 2  
     63 3 0 21 11 65  
     64 5 2 0 1 4  
     65 54 62 12 0 99  
     66 87 64 81 33 0
     67 ```
     68 
     69 Output
     70 
     71 ```
     72 620
     73 ```
     74 
     75 Note
     76 
     77 In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals:
     78 
     79 (*g*23 + *g*32 + *g*15 + *g*51) + (*g*13 + *g*31 + *g*54 + *g*45) + (*g*15 + *g*51) + (*g*54 + *g*45) = 32.