Language: C++

#include<stdio.h>
#include<vector>
#include<algorithm>
#include<functional>

using namespace std;

int n;
char data[500];

vector<pair<int, char>> cnt(26);

int length;

void input(){
	scanf("%d\n", &n);
	gets(data);
	for (int i = 0; i < n; i+=2){
		cnt[data[i] - 'a'].first++;
	}
	sort(cnt.begin(), cnt.end(), greater<pair<int, char> >());
	cnt.push_back(make_pair<int, char>(-1, 0));
}

void process(){
	int curcnt = 0;
	int i;
	for (i = 0; i < 26; ++i) {
		if (cnt[i].first <= 1) break;
		++curcnt;
		if (cnt[i].first != cnt[i + 1].first) {
			if (curcnt >= 3) {
				length += curcnt * 2 + 4;
			}
			else {
				length += curcnt * 4;
			}
			curcnt = 0;
		}
	}
	for (; i < 26; ++i) {
		if (cnt[i].first == 1) {
			length += 2;
		}
		else {
			break;
		}
	}
	length--;
}

void output(){
	printf("%d\n", length);
}

int main() {
	input();
	process();
	output();
	return 0;
}

Compiler output:
a.cc: In function 'void input()':
a.cc:17:14: error: reference to 'data' is ambiguous
   17 |         gets(data);
      |              ^~~~
In file included from /usr/include/c++/14/vector:69,
                 from a.cc:2:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:9:6: note:                 'char data [500]'
    9 | char data[500];
      |      ^~~~
a.cc:17:9: error: 'gets' was not declared in this scope; did you mean 'getw'?
   17 |         gets(data);
      |         ^~~~
      |         getw
a.cc:19:21: error: reference to 'data' is ambiguous
   19 |                 cnt[data[i] - 'a'].first++;
      |                     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:9:6: note:                 'char data [500]'
    9 | char data[500];
      |      ^~~~