# C++ code to get minimum sum of cards after discarding

Suppose we have five numbers in an array T. There are five cards and each card has a number written on them. The ith card has T[i] written on it. We can discard some cards and our goal is to minimize the sum of numbers written on remaining numbers. He is allowed to at most once discard two or three cards with the same number. We won't discard cards if it's impossible to choose two or three cards with the same number. We have to find the minimum sum possible.

So, if the input is like T = [7, 3, 7, 3, 20], then the output will be 26, because removing two cards with a number 7. The remaining sum would be 3 + 3 + 20 = 26.

## Steps

To solve this, we will follow these steps −

n := 5
m := 0
Define an array k of size: 101 and fill with 0
for initialize i := 0, when i < n, update (increase i by 1), do:
a := T[i]
m := m + a
(increase k[a] by 1)
M := m
for initialize i := 0, when i < 101, update (increase i by 1), do:
if k[i] > 1, then:
M := minimum of M and (m - i * (minimum of 3 and k[i]))
return M

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> T){
int n = 5, m = 0, a;
int k = { 0 };
for (int i = 0; i < n; i++){
int a = T[i];
m += a;
k[a]++;
}
int M = m;
for (int i = 0; i < 101; i++)
if (k[i] > 1){
M = min(M, m - i * (min(3, k[i])));
}
return M;
}
int main(){
vector<int> T = { 7, 3, 7, 3, 20 };
cout << solve(T) << endl;
}

## Input

{ 7, 3, 7, 3, 20 }

## Output

26