You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know…
- User
You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array. Input
The first line contains two space-separated integers: n, T (1 ≤ n ≤ 100, 1 ≤ T ≤ 107). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 300).
Output
Print a single number — the length of a sought sequence.
Examples
Input
4 3 3 1 4 2
Output
5
Note
The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.
Assistant
n, T = map(int, input().split()) a = list(map(int, input().split())) # Precompute delta and last for each x delta = [0] * 301 last = [300] * 301 # Initialize to a high value for i in range(n): subarray = a[i:] m = len(subarray) dp = [1] * m for j in range(m): for k in range(j): if subarray[k] <= subarray[j] and dp[j] < dp[k] + 1: dp[j] = dp[k] + 1 max_val = max(dp) if m else 0 current_last = subarray[-1] if m else 0 # Find the smallest possible last element for max_val for j in range(m): if dp[j] == max_val and subarray[j] < current_last: current_last = subarray[j] # Update delta and last for x <= a[i] current_x = a[i] for x in range(1, current_x + 1): if delta[x] < max_val: delta[x] = max_val last[x] = current_last elif delta[x] == max_val and current_last < last[x]: last[x] = current_last # Compute initial states after first copy dp_initial = [1] * n for i in range(n): for j in range(i): if a[j] <= a[i] and dp_initial[i] < dp_initial[j] + 1: dp_initial[i] = dp_initial[j] + 1 initial = [0] * 301 for i in range(n): x = a[i] if dp_initial[i] > initial[x]: initial[x] = dp_initial[i] def compute_sum(x, steps_needed): if steps_needed == 0: return 0 visited = {} path = [] current_x = x sum_total = 0 steps = 0 while True: if current_x in visited: cycle_start = visited[current_x] cycle_length = steps - cycle_start cycle_sum = sum_total - path[cycle_start][1] remaining = steps_needed - cycle_start num_cycles = remaining // cycle_length sum_total = path[cycle_start][1] + num_cycles * cycle_sum remaining_steps = remaining % cycle_length current_x = path[cycle_start][0] for _ in range(remaining_steps): sum_total += delta[current_x] current_x = last[current_x] break if steps >= steps_needed: break visited[current_x] = steps path.append((current_x, sum_total)) sum_total += delta[current_x] steps += 1 current_x = last[current_x] return sum_total max_answer = 0 for x in range(1, 301): if initial[x] == 0: continue total = initial[x] + (compute_sum(x, T-1) if T > 1 else 0) if total > max_answer: max_answer = total print(max_answer)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.