Example Input 6 6 3 1 9 4 3 6 1 2 1 4 2 6 5 4 6 5 3 2 Output 17
- User
Example Input
6 6 3 1 9 4 3 6 1 2 1 4 2 6 5 4 6 5 3 2
Output
17
Assistant
import sys from collections import deque n, m = map(int, sys.stdin.readline().split()) values = [int(sys.stdin.readline()) for _ in range(n)] adj = [[] for _ in range(n + 1)] in_degree = [0] * (n + 1) for _ in range(m): u, v = map(int, sys.stdin.readline().split()) adj[v].append(u) in_degree[u] += 1 dp = [0] * (n + 1) q = deque() for i in range(1, n + 1): if in_degree[i] == 0: q.append(i) dp[i] = values[i - 1] while q: v = q.popleft() for u in adj[v]: if dp[u] < dp[v] + values[u - 1]: dp[u] = dp[v] + values[u - 1] in_degree[u] -= 1 if in_degree[u] == 0: q.append(u) print(max(dp))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.