We need to solve problem: Given a list of integers, we can pick a number ak, delete it and also delete all numbers equal to ak+1 or ak-1. We gain ak points. We can repeat steps. Maximize total points.
This is classic DP problem: Count frequencies of each value. Let's define values from 1 to max value (maybe up to 10^5). For each value i, we have count[i]. If we choose to take value i (i.e., we pick an element of value i and delete all i, i+1, i-1), we can get points i * count[i] but we cannot also take i-1 or i+1. However, careful: When we take value i, we delete all elements equal to i (so we take them all? Wait, the rule says: "choose an element of the sequence (let's denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence. That step brings ak points to the player." That suggests that in a single step, we pick one element (ak) and delete it, and also delete all elements equal to ak+1 and ak-1. We gain ak points (only that element's value? Not multiplied by count of that value). The description: "That step brings ak points to the player." So we only get points equal to the value of the chosen element, not for all occurrences of that value. However, reading examples: Input: 9 numbers: 1 2 1 3 2 2 2 2 3. The note: "Consider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 points." If we choose any element equal to 2 as first step, we delete that one element (the chosen 2) and also delete all elements equal to 1 and 3 (since ak-1=1, ak+1=3). So after that step, the remaining sequence has only the other 2's (those not deleted). But note: The chosen 2 is also deleted (the one we took). So we gain 2 points. Then we have only 2's left; we can then repeatedly pick any 2, but there is no 1 or 3 left, so we delete only the chosen 2 each step? Actually, if we pick a 2, we also delete 1 and 3, but there are none, so only that 2 is removed. So each subsequent step yields 2 points, and we have 4 remaining 2's? Let's check counts: Original: 1 appears 2? Actually list: 1 2 1 3 2 2 2 2 3. Count: 1:2, 2:5, 3:2. First step pick a 2: delete that 2 (chosen) and all 1's and 3's. So we remove both 1's, both 3's, and one 2. Remaining: 2's count = 5 - 1 = 4. Then we have 4 steps, each picking a 2, removing it, gaining 2 points. Total points = 2 + 4*2 = 10. That matches.
But what if we had chosen to take a 2 first, we only gained 2 points, not 2*count[2] (which would be 10). So indeed we get points only for the chosen element, not for all copies of that value. However, note that we could also consider that we could have taken all 2's eventually, but only one at a time.
The problem reduces to: we have a multiset of numbers. In each move, pick a number x, gain x points, remove that occurrence of x, and also remove all occurrences of x-1 and x+1. Goal: maximize sum.
This is essentially a variant of "delete and earn" problem. Since values are up to 10^5, we can use DP on sorted distinct values, but we need to handle frequencies: If we decide to take value i, we can take all occurrences of i? Not exactly; we can only take one per move. But because after taking one occurrence of i, all i-1 and i+1 are removed, we can later take another i? Wait: If we take an occurrence of i, we also delete all occurrences of i-1 and i+1, but do we delete other occurrences of i? The description says: "choose an element of the sequence (let's denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence." It does NOT say that all elements equal to ak are deleted, only the chosen one is deleted. So other copies of i remain. So after taking one i, other i's are still present. However, we also delete all i-1 and i+1. Those are removed entirely (all copies). So effectively, if we ever take any i, we eliminate all i-1 and i+1 from the entire sequence (for future moves). But we can still take other i's later, as long as we haven't deleted them. But note: When we later take another i, will it delete i-1 and i+1 again? They are already gone, so no effect. So we can take all i's one by one, each giving i points. However, we must be careful: If we take an i, it only deletes the chosen i, not all i's. So we can take each i individually. So the total points we can get from all i's is i * count[i], provided we never delete them as side-effect of taking other numbers. But taking i-1 or i+1 will delete all i's because if we take an element with value i-1, it will delete all i (i = (i-1)+1). Similarly, taking i+1 deletes all i. So the presence of i's can be wiped out if we ever take i-1 or i+1.
Thus the problem is exactly: we have values with frequencies. If we decide to "take" (i.e., earn from) all copies of a value i, we can do so by taking them one by one, gaining i * count[i] points. But if we take any value i, we cannot take i-1 or i+1 because those would be deleted either as side-effect of taking i? Wait: If we take i, we delete all i-1 and i+1. So we lose the opportunity to take i-1 and i+1. Conversely, if we take i-1, we delete all i, so we cannot take i. So it's a conflict: choosing to take i precludes taking i-1 and i+1. But we could take i+2 alongside i? Yes, because i+2 is not directly adjacent.
Thus the problem becomes: choose a subset S of values (integers) such that we cannot have two values that differ by 1 simultaneously chosen. For each chosen value i, we get total points = i * count[i]. We want to maximize sum.
But is it exactly that? Consider if we decide to take value i. We will eventually take all copies of i? Could we decide to take only some copies of i and not others? Possibly, but if we take some copies of i, we might still have others left. However, if we are taking i, we are planning to eventually take all copies of i because there is no downside to taking them (they don't conflict with themselves). The only conflict is with adjacent numbers. So the optimal strategy: For each value i, either we take all copies of i (by taking them one by one) or we take none (because taking some but not all would still yield points, but we might as well take all if we are taking any, as they don't conflict with each other). However, there is nuance: taking the first i will delete all i-1 and i+1. After that, we can safely take all remaining i's. So yes, if we decide to include i in the set of values we will earn from, we will take all copies. Because we can do that after eliminating adjacent values.
Thus the problem reduces to: Given array of numbers with frequencies f[i] for i from 1..M (max value). Find maximum sum of selecting a subset of integers such that no two selected integers are consecutive (difference 1). The value for selecting i is i * f[i].
This is classic: DP[i] = max(DP[i-1], DP[i-2] + if[i])? Wait careful: Because if we take i, we cannot take i-1, but we can take i-2. So DP[i] = maximum points considering numbers up to i. Recurrence: DP[i] = max(DP[i-1], DP[i-2] + if[i]). But we also have the possibility of not taking i, then DP[i] = DP[i-1]. However, we must also consider that if we take i, we might have to skip i-1. That is captured. But we also must consider that we might have i-2 and i both taken. That's allowed.
But what about i+1? DP will handle later.
Thus DP is simple. However, we must consider that values may not be contiguous. But we can fill from min to max. For numbers not present, f[i]=0, then DP[i]=DP[i-1] (since i*0=0). So we can just iterate.
But there is also i=1: DP[0]? We'll define DP[0]=0, DP[1]=max(DP[0] + 1f[1], 0)? Actually, DP[1] = max(DP[0], DP[-1]? We'll treat index from 1 to maxVal. DP[0]=0. For i=1, if we take 1, we cannot take 0 (nonexistent). So DP[1] = max(DP[0], 1f[1] + DP[-1]? But we can define DP[-1] as 0. So DP[1] = max(DP[0], 1f[1] + DP[-1]?) But better: recurrence: take i: DP[i-2] + if[i] (since i-1 not allowed). Not take: DP[i-1]. So for i=1, i-2 = -1, we need DP[-1]=0. So DP[1] = max(DP[0], 0 + 1f[1])? Actually DP[-1] is 0, so take = 0 + 1f[1] = 1f[1]. So DP[1] = max(DP[0], 1f[1]). That's fine.
We can implement DP array up to maxVal (up to 100000). But note that n up to 100k and values up to 100k, it's fine.
However, we need to be careful: The DP as described works because we assume that if we take i, we take all occurrences of i, and that yields i * count[i] points. But what about the fact that when we take i, we delete all i-1 and i+1, but we might also delete i+1 later if we haven't taken i+1 yet. In DP we enforce that we cannot take i and i+1 simultaneously. But is it allowed to take i and then later take i+2 without taking i+1? Yes, DP allows that (i and i+2 both selected). So the DP model matches.
But is there any scenario where we might want to take only part of the copies of i to allow something else? Consider that taking i also deletes i-1 and i+1, but if we have some copies of i-1 or i+1, they are all deleted at the moment we first take i. So we cannot later take any i-1 or i+1. However, what if we want to take some copies of i-1, and some copies of i, but not all? Possibly we could intersperse moves: For example, we have numbers: 1,2,2. Count: 1:1, 2:2. If we take the 1 first: we gain 1, and delete all 0 and 2. So the two 2's are deleted, we end with no numbers, total=1. If we take a 2 first: we gain 2, delete all 1 and 3 (the 1 is deleted). Remaining: one 2 left. Then take that 2: gain 2, total=4. So total=4. In DP model, we consider selecting set {2} gives 22=4, selecting {1} gives 11=1, selecting both {1,2} is not allowed because they are adjacent. So optimal is 4, matches. So DP gives correct.
Another example: values: 2,2,3,3. Count: 2:2, 3:2. Options: If we take 2 first: gain 2, delete all 1 and 3 (all 3's are deleted). Remaining: one 2 left. Take it: gain 2, total=4. If we take 3 first: gain 3, delete all 2 and 4 (all 2's deleted). Remaining: one 3 left, take it: gain 3, total=6. So optimal total=6. DP: values: select set {3} gives 32=6; select {2} gives 22=4. So DP picks 6. Works.
Now consider scenario where values might be such that not all copies of a number need to be taken if we take a neighbor? Actually, we can only take numbers that are not deleted. Taking neighbor deletes all copies of the number, so if we want to take both, not possible. So indeed it's a conflict set.
Thus solution: compute frequency array f[1..maxVal] (maybe up to 100000). Let maxV be maximum value in input. Then DP: let dp[i] = max points using numbers up to i. dp[0]=0. For i from 1 to maxV: dp[i] = max(dp[i-1], (i>=2 ? dp[i-2] : 0) + i*f[i]). Then answer dp[maxV].
But careful: The values could be up to 100000, but we need to be cautious about integer overflow? Points: i up to 100000, f[i] up to 100000, product up to 10^10, sum across up to 10^5, but total points up to 10^10, fits in 64-bit. Use long long.
Edge case: values up to 100000, but maybe there is value 0? The input constraint says 1 ≤ ai ≤ 10^5, so min is 1. So we don't have 0. But DP can ignore 0.
Alternatively, we could compress values and sort distinct values, then DP on sorted list. That might be more efficient if maxVal large relative to n, but maxVal is only 100000, which is fine.
Thus simple.
But wait: There is nuance: When we take i, we delete all i-1 and i+1. But what about i-2? They are unaffected. So DP's adjacency constraint only between consecutive numbers. Good.
Thus solution is straightforward.
But is there any scenario where we might want to not take all copies of i because we want to take some but not all? Could there be benefit to taking only some copies of i to leave some for later after taking something else? For example, suppose we have i and i+1 and i+2. If we take i+1, it deletes all i and i+2. So we cannot later take i or i+2. But if we take i, it deletes i+1, but we could still later take i+2. So taking i allows i+2. But if we take i+1, we lose i and i+2. So if we have high count on i+2, we might want to avoid taking i+1. So again, it's a decision about which values to include. But could we partially take i+1, then later take i+2? No, because as soon as we take any i+1, all i+2 are deleted. So we can't take i+2 after taking i+1. Similarly, taking i+2 first would delete i+1? Actually taking i+2 deletes i+1 and i+3, so i+1 would be deleted, preventing later taking i+1. So the conflict is binary: either we take all copies of a value (if we decide to include it) or we take none. So DP works.
Now confirm with some random tests.
Example 1: n=2, [1,2]. f1=1, f2=1. DP[1]=max(dp0,1f1)=1. dp[2]=max(dp1=1, dp0+2f2=0+2=2)=2. answer=2.
Example 2: n=3, [1,2,3]. f1=1, f2=1, f3=1. dp1=1. dp2=max(dp1=1, dp0+2=2) =2. dp3=max(dp2=2, dp1+3=1+3=4)=4. answer=4.
Example 3: f1=2, f2=5, f3=2. maxV=3. dp1=2. dp2=max(dp1=2, dp0+25=10)=10. dp3=max(dp2=10, dp1+32=2+6=8)=10. answer=10.
Thus correct.
So implementation: read n, count frequencies using array of size maybe 100001 or 100005. compute maxVal. Then compute dp array of length maxVal+1 with long long. Use two variables: dp_i_minus_2, dp_i_minus_1, and iterate.
Pseudo:
int n;
cin >> n;
const int MAXV = 100000;
vector cnt(MAXV+1, 0);
int maxV = 0;
for i in 0..n-1:
int x; cin >> x;
cnt[x]++;
maxV = max(maxV, x);
long long dp_im2 = 0; // dp[i-2]
long long dp_im1 = 0; // dp[i-1]
for i = 1..maxV:
long long take = dp_im2 + i * cnt[i];
long long notake = dp_im1;
long long dpi = max(take, notake);
// shift
dp_im2 = dp_im1;
dp_im1 = dpi;
answer = dp_im1;
But careful: dp_im1 initial for i=0? Let's set dp_im2 = 0 for i-2, dp_im1 = 0 for i-1 when i=1? Actually for i=1, i-2 = -1 -> dp_{-1}=0, i-1=0 -> dp_0=0. So we can start with dp_prev = 0 (dp[i-1]), dp_prev2 = 0 (dp[i-2]). Then loop i=1 to maxV: dpi = max(dp_prev, dp_prev2 + i*cnt[i]). Then update dp_prev2 = dp_prev; dp_prev = dpi. At end answer = dp_prev.
Edge: If maxV=0? But min=1, so maxV>=1.
Alternative: Could also use array of size maxV+1 for clarity.
Complexities: O(maxV) which is at most 100k, fine.
But note that values can be up to 10^5, but if we have values like 100000, we need array size 100001. That's okay.
Check memory: cnt array of long long 100001 * 8 = 0.8 MB, fine.
Thus solution done.
However, sometimes this problem is known as "Codeforces 455A - Boredom". Yes, it's Codeforces 455A. The solution exactly as described.
Now we need to provide final answer with explanation and code.
But the user asked: "Alex doesn't like boredom... help him." So we need to output a single integer. So code is straightforward.
Now double-check: Could there be any trap? Input values up to 100k, but maybe the maximum value can be more than 100k? Problem says 1 ≤ ai ≤ 10^5. So yes.
One more nuance: The DP approach works because frequencies are independent and we assume we take all copies of a selected value. But is it always optimal to take all copies of a value if we decide to take that value? Suppose we have count[i] huge, but taking i also deletes i-1 and i+1. But maybe taking some but not all copies of i could allow us to also take i+1? But taking any copy of i deletes all i+1 regardless of how many i's we take? The rule: "choose an element... and delete it, at that all elements equal to ak+1 and ak-1 also must be deleted." So if we pick ak = i, then we delete that one i, and all elements equal to i+1 and i-1. So after this move, all i+1 and i-1 are gone. So we cannot later take i+1. So if we ever take any i, we lose all i+1 and i-1. So even if we only take one i, we lose i+1 entirely. So no benefit to not taking all i's, because we already lost i+1 anyway. But could we take some i's, then later take i+1 after? No, because i+1 are all deleted at the first i taken. So we cannot. So if we decide to include i in our selected set, we might as well take all i's because they are not conflicting with each other. There is no downside to taking all i's; we get more points. So optimal to take all copies.
Thus DP correct.
Therefore we can produce final answer.
Now provide code.
Implementation in C++:
#include
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
const int MAXA = 100000;
vector cnt(MAXA+1, 0);
int maxV = 0;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
cnt[x]++;
maxV = max(maxV, x);
}
long long dp_prev2 = 0; // dp[i-2]
long long dp_prev1 = 0; // dp[i-1]
for (int i = 1; i <= maxV; ++i) {
long long take = dp_prev2 + i * cnt[i];
long long notake = dp_prev1;
long long dpi = max(take, notake);
dp_prev2 = dp_prev1;
dp_prev1 = dpi;
}
cout << dp_prev1 << endl;
return 0;
}
That solves.
But we might consider using long long for cnt and product. cnt can be up to 10^5, product up to 10^10, fits in long long.
Edge Cases: If n=0? Not possible as n>=1.
Test: n=1, a=5: cnt[5]=1, maxV=5. DP from 1..5: For i<5, cnt[i]=0, so dp will propagate zeros. At i=5, take = dp_prev2 + 5*1; dp_prev2 at i=5: after i=4, dp_prev1 maybe? Actually compute: i=1: cnt[1]=0 -> dpi = max(dp_prev2 (0), dp_prev1(0)) = 0, dp_prev2=0, dp_prev1=0.
i=2: similar, 0.
i=3: 0.
i=4: 0.
i=5: dp_prev2 = dp_prev1 at i=4 = 0; dp_prev1 = dp_i at i=4 = 0; take = 0+5=5; notake=0; dpi=5. answer=5. Good.
Now complexity O(maxV) ~ 100k.
All good.
But some solutions might use map to compress values to avoid iterating over up to 100k, but it's fine.
Thus final answer.
Now write final response: include explanation and code. ```cpp
#include
#include
#include
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
const int MAX_VAL = 100000;
vector<long long> cnt(MAX_VAL + 1, 0);
int maxV = 0;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
cnt[x]++;
if (x > maxV) maxV = x;
}
// dp[i] = best score considering numbers 1..i
// We only need the last two values.
long long dp_im2 = 0; // dp[i-2]
long long dp_im1 = 0; // dp[i-1]
for (int i = 1; i <= maxV; ++i) {
long long take = dp_im2 + i * cnt[i];
long long notake = dp_im1;
long long dpi = max(take, notake);
dp_im2 = dp_im1;
dp_im1 = dpi;
}
cout << dp_im1 << endl;
return 0;
}