Two friends decided to play a very exciting online card game. At the beginning of this game, each player gets a deck of cards, in which each card has some strength assigned. After that, each player picks a random card from his deck and they compare the strengths of the picked cards. The player who picked a card with larger strength wins. There is no winner in case both players picked cards with equal strength.
The first friend got a deck with n cards. The i-th his card has strength . The second friend got a deck with m cards. The i-th his card has strength .
The first friend wants to win very much. So he decided to improve his cards. He can increase by 1 the strength of any card by 1 dollar. Any card can be improved as many times as he wants. The second friend can't improve his cards because he doesn't know about this possibility.
What is the minimum amount of money which the first player needs to guarantee a victory for himself?
Example:
Input: n = 3, a = [1,3,10], m = 2 , b = [3,4]
Output: 6
Approach
C++
#include <bits/stdc++.h>using namespace std;long long cardGame(long long n, long long a[],long long m, long long b[]){long long max1 = 0;for (long long j = 0; j < m; j++){max1 = max(max1, b[j]);}long long res = 0;max1++;for (long long i = 0; i < n; i++){if (a[i] < max1)res += max1 - a[i];}return res;}int main(){long long n = 3;long long a[n] = {1, 3, 10};long long m = 2;long long b[m] = {3, 4};cout << cardGame(n, a, m, b) << "\n";return 0;}
No comments:
Post a Comment