#K81977. Maximum Sum Path Through Rooms

    ID: 35873 Type: Default 1000ms 256MiB

Maximum Sum Path Through Rooms

Maximum Sum Path Through Rooms

You are given a directed graph representing a set of rooms and one-way doors between them. Each room is assigned a positive integer value. Starting from a given room S, your task is to traverse through the rooms by following the doors and collect the sum of the room values along the path. You cannot revisit a room during a single traversal.

The path sum is defined as:

[ \text{Sum} = \sum_{i \in \text{path}} \text{roomValue}[i] ]

Your task is to determine the maximum sum obtainable by any valid path starting from room S.

Note: The graph may contain cycles; however, you must not revisit any room in a single path.

inputFormat

The input is given from the standard input (stdin) in the following format:

N S
v1 v2 ... vN
k1 r1 r2 ... rk1
k2 r1 r2 ... rk2
...
kN r1 r2 ... r kN

Here:

  • N is the number of rooms.
  • S is the starting room number (1-indexed).
  • The second line contains N integers, where the i-th integer represents the value of the i-th room.
  • Each of the next N lines describes the doors from room i. The first integer in each line ki denotes the number of doors from room i, followed by ki integers indicating the destination room numbers.

outputFormat

Print a single integer to the standard output (stdout) representing the maximum sum obtainable by a valid path starting from room S.

## sample
5 1
2 3 5 1 4
1 2
1 3
1 4
1 5
0
15