Table of Contents

ExplanationImplementation

Official Editorial (C++)

Explanation

We can find the maximum experience point available by comparing the largest available experience point gained by completing one more quest or not.

Implementation

Time Complexity: O(min(n,k))\mathcal{O}(\min(n,k)) for each test case

C++

#include <bits/stdc++.h>
using namespace std;
int main() {
int test_num;
cin >> test_num;
for (int t = 0; t < test_num; t++) {
int n;
int k;
cin >> n >> k;

Python

for _ in range(int(input())):
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
best_second = b[0]
first_total = a[0]
max_score = a[0] + best_second * (k - 1)
for z in range(1, min(n, k)):
best_second = max(best_second, b[z])
# compare experience point of completing one more quest or not
max_score = max(max_score, first_total + a[z] + best_second * (k - z - 1))
first_total += a[z]
print(max_score)

Java

import java.io.*;
import java.util.*;
public class Quests {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int testNum = Integer.parseInt(read.readLine());
for (int t = 0; t < testNum; t++) {
StringTokenizer initial = new StringTokenizer(read.readLine());
int n = Integer.parseInt(initial.nextToken());

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!