Alice and Bob are very good friends. They keep on playing some strange games. Today Alice came up with a new game, in which each player gets N cards at the start. Each card has its value printed on it.
The game proceeds as each player chooses a random card and shows its value. The player having a card with the higher value wins. As Alice came up with this game, he wants to ensure his win. So he starts to increase the value of some cards using an algorithm. To increase the value of a card by
, the running time of the algorithm is K seconds.
Find the minimum running time of the algorithm, ensuring the win of Alice.Example:
Input: n = 5, k = 3, a = [8, 9, 10, 23, 4], b = [7, 9, 2, 3, 13]
Output: 75
Approach
C++
#include <bits/stdc++.h>using namespace std;long long strangeGame(long long n, long long k,long long a[], long long b[]){long long max1 = INT_MIN;for (long long j = 0; j < n; j++){max1 = max(max1, b[j]);}max1 = max1 + 1;long long res = 0;for (long long i = 0; i < n; i++){if (a[i] < max1){res += (max1 - a[i]) * k;}}return res;}int main(){long long n = 5, k = 3;long long a[n] = {8, 9, 10, 23, 4};long long b[n] = {7, 9, 2, 3, 13};cout << strangeGame(n, k, a, b) << "\n";return 0;}
Java
public class StrangeGame {public static void main(String[] args) {int n = 5, k = 3;int a[] = { 8, 9, 10, 23, 4 };int b[] = { 7, 9, 2, 3, 13 };System.out.println(strangeGame(n, k, a, b));}static int strangeGame(int n, int k, int a[], int b[]) {int max1 = Integer.MIN_VALUE;for (int j = 0; j < n; j++) {max1 = Math.max(max1, b[j]);}max1 = max1 + 1;int res = 0;for (int i = 0; i < n; i++) {if (a[i] < max1) {res += (max1 - a[i]) * k;}}return res;}}
No comments:
Post a Comment