2252 A. Boss Fight
A greedy approach to maximizing card usage before an adaptive shield triggers.
Problem Overview
You are fighting a boss and have an array of spell cards, each dealing a certain amount of damage. You can play them in any order you choose.
The catch? If you play two identical cards in a row, the boss's adaptive shield permanently activates. The card that triggers the shield still deals damage, but every card you play after it will deal 0 damage.
We need to find the optimal order to play our cards to maximize the total damage dealt.
Reveal Hint▼
If you want to play as many cards as possible before the shield activates, how can you use your unique cards to keep your most frequent cards separated?
The Naive Approach
When first looking at this, you might think about just sorting the cards or playing them blindly.
Let's imagine we have a hand of cards: five 10s, two 5s, and one 2.
If we just group them together or play them haphazardly as [10, 10, 5, 5, 2, 10, 10, 10], we run into an immediate disaster:
Naive Play: Shield triggers immediately on the second 10!
Because the shield triggers on the second 10, the remaining 6 cards in our hand are rendered completely useless and will deal 0 damage. We lost a massive amount of potential damage.
Maximizing the Score
The absolute worst thing we can do is trigger the shield early. To maximize our score, we should only ever play two identical cards at the very end of our hand.
Because there are no cards left after the final trigger, the 0-damage penalty does not affect us!
The Interleaving Strategy
To delay the shield as long as possible, we must avoid playing identical cards back-to-back. If we have a single card that appears a lot (like 10), we can use our other cards (like 5 and 2) as "spacers" between the 10s.
But why should we start our sequence with our most frequent card (10) instead of a spacer (5)?
If we start with a spacer and alternate (5, 10, 5, 10), our two spacers only allow us to place two 10s.
But if we start with 10 (10, 5, 10, 5, 10), those exact same two spacers allow us to fit three 10s!
Key Observation
Starting with the most frequent card essentially gives us a "free" slot at the very beginning of the sequence, maximizing the total number of cards we can play.
Let's try interleaving our hand:
Step 1: Start with the most frequent card
We place our 10s first, using our 5s and 2s as single spacers between them to avoid the shield.
Step 2: The Final Pair
We append our final 10 to the end. It touches the previous 10, triggering the shield, but we've successfully played ALL our cards!
This illustrates our optimal playing method: greedily interleave the most frequent card using all other cards as spacers, and dump up to two copies of the most frequent card at the very end.
Hitting the Limit
Can we always play every single card in our hand? Not always. It depends entirely on whether we run out of 10s first, or if we run out of spacers first.
If one card appears too many times, we will eventually run out of spacers. When that happens, what is the absolute maximum total number of cards we can play before they start dealing 0 damage?
Let's build the longest possible valid sequence visually. Imagine we have exactly 2 spacers (a 5 and a 2), and a huge stack of 10s:
1. The Start (+1)
We place one free 10 at the very beginning.
2. Spacers & Matches (2 × Spacers)
For every spacer we play, we can safely attach exactly one 10 after it. Since every spacer brings a matching 10 with it, this step always contributes (2 × Spacers) to our total card count.
3. The End (+1)
We place one final 10 at the very end to intentionally trigger the shield without penalty.
By adding these steps together, we see exactly where the math comes from. The core of our sequence is built out of (Spacer + Match) pairs, which is why we multiply our spacers by two. Then we simply add 1 for the start, and 1 for the end!
Key Observation
The absolute maximum total number of cards that deal damage is exactly (2 × number of spacers) + 2. Keep in mind, this maximum is not always achievable (we might run out of cards first!), it is simply an absolute ceiling that we cannot exceed.
The Two Scenarios
-
Scenario 1: We have enough spacers to separate everything If the total count of cards in our hand is smaller than or equal to this limit, we will successfully place all of them before we run out of spacers. But what if we have extra spacers left over? We just tuck them right next to our other spacers! For example, if we have two
10s, two5s, and two2s, we don't just stop at10, 5, 10, 5. We can tuck the leftover2s right next to the5s:10, 5, 2, 10, 5, 2.(Note: Because our main card is strictly the most frequent card in our hand, no spacer can ever appear more times than it. This mathematically guarantees we will always have enough gaps to distribute identical spacers so they never touch each other!)
Before: Interleave the 10s
We have two
10s, two5s, and two2s. First, we interleave all the10s with the5s.0101521035After: Pack the leftovers
We have successfully placed all our
10s, but the two2s remain. We simply pack them right next to the5s! Since5and2are different, no identical cards ever touch.01015223104552- Damage Dealt: The sum of all cards in our hand.
-
Scenario 2: The most frequent card overwhelms the spacers If the total count of cards in our hand exceeds this limit, we will run out of
5s and2s to separate the10s. We simply play up to our limit, intentionally triggering the shield on the final pair. Any cards that exceed this limit are forced out after the shield triggers, so they deal 0 damage.Scenario 2: Not Enough Spacers
We have four 10s and one 5 (5 cards total). Our limit is (2 × 1 spacer) + 2 = 4 total cards. We successfully play exactly 4 cards (triggering the shield on the 4th). The 5th card is forced out after the shield, dealing 0 damage (unhighlighted).
01015210310410- Damage Dealt: The sum of all our spacer cards, plus exactly (number of spacers + 2) copies of the most frequent card.
Pseudocode
Before writing the final code, let's break down the logic step-by-step. We just need to find the most frequent card and compare it against the total number of spacers!
1. Find the most frequent card:
- Loop through the hand of cards.
- Add each card's value to our total damage counter.
- Track the frequency of each card using a hash map.
- Keep a record of the absolute highest frequency we've seen (max_freq) and what card that was (max_freq_val).
2. Count our available spacers:
- Subtract our max_freq from the total number of cards in our hand. This gives us our `spacers` count.
3. Check Scenario 1 (We have enough spacers):
- If max_freq is less than or equal to (spacers + 2):
- We can safely play every single card! Output the total damage we calculated in step 1.
4. Check Scenario 2 (The most frequent card overwhelms the spacers):
- If max_freq is greater than (spacers + 2):
- Calculate the damage from all the spacer cards (total_damage - (max_freq * max_freq_val)).
- We are allowed to play exactly (spacers + 2) copies of our frequent card.
- Add the spacer damage to the allowed frequent card damage and output the sum.Solution Code
#include <iostream>
#include <vector>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> freq(1005, 0);
long long total_sum = 0;
int max_freq = 0;
long long max_freq_val = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
freq[a]++;
total_sum += a;
if (freq[a] > max_freq) {
max_freq = freq[a];
max_freq_val = a;
}
}
int spacers = n - max_freq;
if (max_freq <= spacers + 2) {
cout << total_sum << "\n";
} else {
long long others_sum = total_sum - (long long)max_freq * max_freq_val;
cout << others_sum + (long long)(spacers + 2) * max_freq_val << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}