Tap here to switch tabs
Problem
Submissions

Problem: Maximum Score of a Node Sequence

Try to solve the Maximum Score of a Node Sequence problem.

Statement

You are given an undirected graph with nn nodes, numbered from 00 to n−1n - 1. You are provided with a 00 indexed integer array scores of length nn, where scores[i] represents the score of node ii, and a 2D2 {D} integer array edges, where edges[i] =[ui,vi]= [u_i, v_i] denotes an undirected edge between nodes uiu_i and viv_i.

A valid node sequence is defined as a sequence of 44 distinct nodes [a,b,c,d][a, b, c, d] such that an edge exists between every consecutive pair of nodes in the sequence: (a,b)(a, b), (b,c)(b, c), and (c,d)(c, d).

The score of a node sequence is the sum of the scores of the 44 nodes in that sequence.

Return the maximum possible score of a valid node sequence. If no valid node sequence of length 44 exists, return −1-1.

Constraints:

  • n ==== scores.length

  • 4≤n≤5×1044 \leq n \leq 5 \times 10^4

  • 1≤1 \leq scores[i] ≤108\leq 10^8

  • 0≤0 \leq edges.length ≤5×104\leq 5 \times 10^4

  • edges[i].length ==2== 2

  • 0≤ai,bi≤n−10 \leq a_i, b_i \leq n - 1

  • ai≠bia_i \neq b_i

  • There are no duplicate edges.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Score of a Node Sequence

Try to solve the Maximum Score of a Node Sequence problem.

Statement

You are given an undirected graph with nn nodes, numbered from 00 to n−1n - 1. You are provided with a 00 indexed integer array scores of length nn, where scores[i] represents the score of node ii, and a 2D2 {D} integer array edges, where edges[i] =[ui,vi]= [u_i, v_i] denotes an undirected edge between nodes uiu_i and viv_i.

A valid node sequence is defined as a sequence of 44 distinct nodes [a,b,c,d][a, b, c, d] such that an edge exists between every consecutive pair of nodes in the sequence: (a,b)(a, b), (b,c)(b, c), and (c,d)(c, d).

The score of a node sequence is the sum of the scores of the 44 nodes in that sequence.

Return the maximum possible score of a valid node sequence. If no valid node sequence of length 44 exists, return −1-1.

Constraints:

  • n ==== scores.length

  • 4≤n≤5×1044 \leq n \leq 5 \times 10^4

  • 1≤1 \leq scores[i] ≤108\leq 10^8

  • 0≤0 \leq edges.length ≤5×104\leq 5 \times 10^4

  • edges[i].length ==2== 2

  • 0≤ai,bi≤n−10 \leq a_i, b_i \leq n - 1

  • ai≠bia_i \neq b_i

  • There are no duplicate edges.