Skip to Content

Quests

Editorial oficial (C++) 

Explicación

Podemos hallar el máximo de puntos de experiencia disponible comparando el mayor puntaje de experiencia que se obtiene al completar una misión más o no.

Implementación

Complejidad temporal: O(min(n,k))\mathcal{O}(\min(n,k)) para cada caso de prueba

#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; vector<int> a(n); vector<int> b(n); for (int x = 0; x < n; x++) { cin >> a[x]; } for (int y = 0; y < n; y++) { cin >> b[y]; } int best_second = b[0]; int first_total = a[0]; int max_score = a[0] + best_second * (k - 1); for (int z = 1; z < min(n, k); z++) { best_second = max(best_second, b[z]); // comparamos los puntos de experiencia de completar una misión más o no max_score = max(max_score, first_total + a[z] + best_second * (k - z - 1)); first_total += a[z]; } cout << max_score << "\n"; } }
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]) # comparamos los puntos de experiencia de completar una misión más o no max_score = max(max_score, first_total + a[z] + best_second * (k - z - 1)) first_total += a[z] print(max_score)
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()); int k = Integer.parseInt(initial.nextToken()); int[] a = Arrays.stream(read.readLine().split(" ")) .mapToInt(Integer::parseInt) .toArray(); int[] b = Arrays.stream(read.readLine().split(" ")) .mapToInt(Integer::parseInt) .toArray(); int best_second = b[0]; int first_total = a[0]; int max_score = a[0] + best_second * (k - 1); for (int z = 1; z < Math.min(n, k); z++) { best_second = Math.max(best_second, b[z]); // comparamos los puntos de experiencia de completar una misión más o no max_score = Math.max(max_score, first_total + a[z] + best_second * (k - z - 1)); first_total += a[z]; } System.out.println(max_score); } } }