Problem:
C - No Palindromes
問題文
umg くんは長さ $N$ の英小文字からなる文字列 $S$ を持っています。
umg くんは回文が苦手なので、$S$ の文字を自由に並び替えて、次の条件を満たす文字列 $T$ を作ることにしました。
$T$ のどの長さ $2$ 以上の連続する部分文字列も回文ではない。
このような $T$ が存在するかどうか判定し、存在するならば $1$ つ求めてください。
入力
入力は以下の形式で標準入力から与えられる。
$N$
$S$
制約
$1 \leq N \leq 10^5$
$S$ は英小文字からなる長さ $N$ の文字列である。
出力
条件を満たす文字列 $T$ が存在しない場合は、
-1
を出力せよ。
そうでない場合は、$T$ を $1$ つ出力せよ。
条件を満たす $T$ は複数存在するかもしれないが、それらのうちのどれを出力しても正答となる。
入力例 1
3
aba
出力例 1
-1
$S =$
aba
を並び替えて作れる文字列は次の $3$ つです。
aab
部分文字列
aa
は回文である。
aba
部分文字列
aba
は回文である。
baa
部分文字列
aa
は回文である。
これらはすべて条件を満たさないので、$T$ は存在しません。
入力例 2
6
bamboo
出力例 2
boamob
boamob
のどの連続する長さ $2$ 以上の部分文字列も回文ではないので、これが $T$ の $1$ つとなります。
入力例 3
14
takeyabuyaketa
出力例 3
takeyabuyaketa

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n;
  cin>>n;
  string s;
  cin>>s;

  vector<Int> cnt(26,0);
  for(char c:s) cnt[c-'a']++;

  string ans;
  ans+='?';
  ans+='!';
  for(Int i=0;i<n;i++){
    using P = pair<Int, char>;
    vector<P> vp;
    for(Int j=0;j<26;j++)
      vp.emplace_back(cnt[j],'a'+j);
    sort(vp.rbegin(),vp.rend());
    for(auto p:vp){
      if(p.first==0) continue;
      if(ans[i+0]==p.second) continue;
      if(ans[i+1]==p.second) continue;
      ans+=p.second;
      cnt[p.second-'a']--;
      break;
    }
    if((Int)ans.size()!=i+3) drop(-1);
  }
  ans.erase(ans.begin());
  ans.erase(ans.begin());

  cout<<ans<<newl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3840
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }


string solve(int N, const string &S) {
    map<char,int> ma;
    for (auto c : S) ma[c]++;
    if (ma.size() == 1) {
        if (N == 1) return S;
        else return "-1";
    }
    else if (ma.size() == 2) {
        if (N == 2) return S;
        else return "-1";
    }
    else {
        string res = "";
        while (!ma.empty()) {
            int vmax = -1;
            char pmax;
            for (auto it : ma) {
                if (res.size() > 0 && res.back() == it.first) continue;
                if (res.size() > 1 && res[res.size() - 2] == it.first) continue;
                if (chmax(vmax, it.second)) pmax = it.first;
            }
            if (vmax == -1) return "-1";
            res += pmax;
            ma[pmax]--;
            if (ma[pmax] == 0) ma.erase(pmax);
        }
        return res;
    }
}

int main() {
    int N;
    string S;
    cin >> N >> S;
    cout << solve(N, S) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3440
final_rank: 2

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long long INF = 1e18;
//const ll mod = 1000000007;
ll N;
string S;
map<char, ll> mp;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    cin >> S;
    for(int i = 0; i < N; i++) {
        mp[S[i]]++;
    }
    string ans;
    for(int i = 0; i < N; i++) {
        char tmp = '#';
        for(char a = 'a'; a <= 'z'; a++) {
            if(i >= 2 and ans[i-2] == a) continue;
            if(i >= 1 and ans[i-1] == a) continue;
            if(mp[tmp] < mp[a]) tmp = a;
        }
        if(tmp == '#') {
            cout << -1 << endl;
            return 0;
        }
        ans.push_back(tmp);
        mp[tmp]--;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3628
final_rank: 7

code_snippet: // Sに含まれるアルファベットのうち
// 過半数以上のアルファベット文字があったらだめ
// そうじゃなかったら絶対作れそう？
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  string S;
  cin >> N >> S;
  vector<int> alphabet('z' - 'a' + 1, 0);
  for(char ch: S) ++alphabet[ch - 'a'];
  // for(int num: alphabet) {
  //   if(num > N/2) {
  //     cout << -1 << endl;
  //     return 0;
  //   }
  // }

  // 構築
  // 同じ文字が隣りあってはダメ
  // 長さ3→両端同じはだめ
  string res = "";
  
  // int r = 0;
  // for(int i = 0; i < N; ++i) {
  //   while(alphabet[r] == 0) {
  //     r = (r + 1)%('z' - 'a' + 1);
  //   }
  //   res += (char)('a' + r);
  //   --alphabet[r];
  //   r = (r+1)%('z' - 'a' + 1);
  // }

  vector<pair<int,int>> alp('z' - 'a' + 1);
  for(int i = 0; i < 'z' - 'a' + 1; ++i) {
    alp[i].first = alphabet[i];
    alp[i].second = i;
  }
  sort(alp.rbegin(), alp.rend());
  for(int i = 0; i < N; ++i) {
    set<char> prev;
    if(i-1 >= 0) prev.insert(res[i-1]);
    if(i-2 >= 0) prev.insert(res[i-2]);
    bool flg = false;
    for(int r = 0; r < 'z' - 'a' + 1; ++r) {
      if(alp[r].first > 0 && prev.find((char)(alp[r].second + 'a')) == prev.end()) {
        res += (char)(alp[r].second + 'a');
        --alp[r].first;
        flg = true;
        break;
      }
    }
    if(!flg) {
      cout << -1 << endl;
      return 0;
    }
    sort(alp.rbegin(), alp.rend());
  }

  for(int i = 0; i < N-1; ++i) {
    if(res[i] == res[i+1]) {
      cout << -1 << endl;
      return 0;
    }
  }
  for(int i = 0; i < N-2; ++i) {
    if(res[i] == res[i+2]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << res << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3452
final_rank: 5

code_snippet: #pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

struct info{
	char c = ' ';
	int idx = 0;
	int cnt = 0;
};

int N;
string S;

void input(void){
	cin >> N >> S;
}

int main(void){
	input();
	string res = "";
	vector<info> infos(26);
	for (int i = 0; i < 26; ++i){
		infos[i].c = (char)('a' + i);
	}
	for (int i = 0; i < N; ++i){
		int idx = S[i] - 'a';
		infos[idx].cnt++;
	}

	priority_queue<info, vector<info>, function<bool(info, info)>> waiting(
		[](info i1, info i2){ return i1.idx > i2.idx; }
	);
	priority_queue<info, vector<info>, function<bool(info, info)>> ok(
		[](info i1, info i2){ return i1.cnt < i2.cnt; }
	);

	for (auto i : infos)
		if (i.cnt)
			ok.push(i);

	for (int i = 0; i < N; ++i){
		while (waiting.size()){
			info t = waiting.top();
			if (t.idx <= i){
				ok.push(t);
				waiting.pop();
			}
			else break;
		}
		if (ok.size() == 0){
			cout << -1 << endl;
			return 0;
		}
		info t = ok.top();
		ok.pop();
		res += t.c;
		t.cnt--;
		t.idx = i + 3;
		if (t.cnt)
			waiting.push(t);
	}

	cout << res << endl;

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3440
final_rank: 2

code_snippet: #line 2 "cpplib/util/template.hpp"
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx")
#include<bits/stdc++.h>
using namespace std;
struct __INIT__{__INIT__(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}__INIT__;
typedef long long lint;
#define INF (1LL<<60)
#define IINF (1<<30)
#define EPS (1e-10)
#define endl ('\n')
typedef vector<lint> vec;
typedef vector<vector<lint>> mat;
typedef vector<vector<vector<lint>>> mat3;
typedef vector<string> svec;
typedef vector<vector<string>> smat;
template<typename T>inline void numout(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<<i<INF/2?i:"INF";f=1;}cout<<endl;}
template<typename T>inline void numout2(T t){for(auto i:t)numout(i);}
template<typename T>inline void output(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<<i;f=1;}cout<<endl;}
template<typename T>inline void output2(T t){for(auto i:t)output(i);}
template<typename T>inline void _output(T t){bool f=0;for(lint i=0;i<t.size();i++){cout<<f?"":" "<<t[i];f=1;}cout<<endl;}
template<typename T>inline void _output2(T t){for(lint i=0;i<t.size();i++)output(t[i]);}
#define rep(i,...) for(auto i:range(__VA_ARGS__)) 
#define rrep(i,...) for(auto i:reversed(range(__VA_ARGS__)))
#define repi(i,a,b) for(lint i=lint(a);i<(lint)(b);++i)
#define rrepi(i,a,b) for(lint i=lint(b)-1;i>=lint(a);--i)
#define irep(i) for(lint i=0;;++i)
inline vector<long long> range(long long n){vector<long long>v(n);iota(v.begin(),v.end(),0LL);return v;}
inline vector<long long> range(long long a,long long b){vector<long long>v(b-a);iota(v.begin(),v.end(),a);return v;}
inline vector<long long> range(long long a,long long b,long long c){if((b-a+c-1)/c<=0)return vector<long long>();vector<long long>v((b-a+c-1)/c);for(int i=0;i<(int)v.size();++i)v[i]=i?v[i-1]+c:a;return v;}
template<typename T>inline T reversed(T v){reverse(v.begin(),v.end());return v;}
#define all(n) begin(n),end(n)
template<typename T,typename E>bool chmin(T& s,const E& t){bool res=s>t;s=min<T>(s,t);return res;}
template<typename T,typename E>bool chmax(T& s,const E& t){bool res=s<t;s=max<T>(s,t);return res;}
const vector<lint> dx={1,0,-1,0,1,1,-1,-1};
const vector<lint> dy={0,1,0,-1,1,-1,1,-1};
#define SUM(v) accumulate(all(v),0LL)
template<typename T,typename ...Args>auto make_vector(T x,int arg,Args ...args){if constexpr(sizeof...(args)==0)return vector<T>(arg,x);else return vector(arg,make_vector<T>(x,args...));}
#line 2 "code.cpp"

int main(){
    lint n;
    cin>>n;
    string s;
    cin>>s;
    map<char,lint>m;
    rep(i,n){
        m[s[i]]++;
    }
    multiset<pair<char,lint>>v;
    sort(all(s),[&](auto s,auto t){return m[s]==m[t]?s>t:m[s]>m[t];});
    string ans(n,'#');
    vector<lint>b(n,0);
    lint now=0;
    rep(i,n){
        while(b[now%n])now++;
        ans[now%n]=s[i];
        b[now%n]=1;
        now+=3;
    }
    for(int i=0;i<n-1;++i){
        if(ans[i]==ans[i+1]){
            cout<<-1<<endl;
            return 0;
        }
    }
    for(int i=0;i<n-2;++i){
        if(ans[i]==ans[i+2]){
            cout<<-1<<endl;
            return 0;
        }
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4764
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (int) (n); i++)
#define reps(i, n) for (int i = 1; i <= (int) (n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end())
#define bit(n) (1LL << (n))
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template<typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr int DX[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int sign(int x) { return (x > 0) - (x < 0); }
int gcd(int a, int b) {
    while (b) { swap(a %= b, b); }
    return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int cdiv(int a, int b) { return (a - 1 + b) / b; }
template<typename T> void fin(T mes) {
    cout << mes << endl;
    exit(0);
}
template<typename T> T sq(T x) { return x * x; }
template<typename T, typename U> bool chmax(T &a, const U &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> bool chmin(T &a, const U &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
    os << "(" << rhs.first << ", " << rhs.second << ")";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const deque<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const set<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
struct setup {
    static constexpr int PREC = 20;
    setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} setup;

signed main() {
    int N;
    string S;
    cin >> N >> S;
    vint ap(26);
    for (char c:S) { ap[c - 'a']++; }
    string T;
    while (N--) {
        vector<char> taboo;
        if (T.size() >= 1) { taboo.emplace_back(T[T.size() - 1]); }
        if (T.size() >= 2) { taboo.emplace_back(T[T.size() - 2]); }
        int m = 0, a = -1;
        rep(i, 26) {
            bool flg = false;
            for (char c:taboo) { if (i + 'a' == c) { flg = true; }}
            if (flg) { continue; }
            if (chmax(m, ap[i])) { a = i; }
        }
        if (a == -1) { fin(-1); }
        ap[a]--;
        T.push_back(a + 'a');
    }
    cout << T << endl;

}
accuracy: 1
time_ms: 10
memory_kb: 3812
final_rank: 8

code_snippet: #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>

#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

//#include<atcoder/all>

using namespace std;

//using namespace atcoder;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << endl
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << " \n"[i==(int)v.size()-1]
template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
void Yes(bool cond) { cout << (cond ? "Yes" : "No") << '\n'; }
void YES(bool cond) { cout << (cond ? "YES" : "NO") << '\n'; }



void solve() {
	int N; string S; cin >> N;
	cin >> S;
	//map<char, int>mp;

	vector<int>cnt(26);
	for (char c : S)cnt[c-'a']++;
	//for (char i = 'a'; i <= 'z'; i++) {
	//	cnt[i - 'a'] = mp[i];
	//}
	vector<pair<int, char>>v(26);
	REP(i, 26) {
		//cout << cnt[i] << " " << (char)('a' + i) << endl;
		v[i] = { cnt[i],(char)('a'+i) };
	}
	string ans;
	REP(j, N) {
		sort(v.rbegin(), v.rend());
		bool ok = false;
		REP(i, 26) {
			//cout << v[i].first << " " << v[i].second << endl;
		}
		REP(i, 26) {
			if (v[i].first == 0)break;
			if (j - 1 >= 0 && ans[j - 1] == v[i].second)continue;
			if (j - 2 >= 0 && ans[j - 2] == v[i].second)continue;
			v[i].first--;
			ans += v[i].second;
			//print(v[i].second);
			ok = true;
			break;
		}
		if (!ok) {
			print(-1); return;
		}
	}
	print(ans);
}



signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	//int q;
	//cin >> q;
	//while (q--)
	solve();

}
accuracy: 1
time_ms: 20
memory_kb: 3616
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

using i64 = long long;
using P   = pair<i64, i64>;

#define overload3(_1, _2, _3, name, ...) name
#define rep1(i, n) for(i64 i = 0LL; i < (n); ++i)
#define rep2(i, a, b) for(i64 i = (a); i < (b); ++i)
#define rep(...) overload3(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define all(v) v.begin(), v.end()

struct IoSetup {
    IoSetup() {
        // cin.tie(nullptr);
        // ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
        cerr << fixed << setprecision(10);
    }
} iosetup;

int main() {
    i64 N;
    cin >> N;
    string S;
    cin >> S;
    vector<i64> C(26);
    rep(i, N) C[S[i] - 'a']++;
    priority_queue<P> que, tmp;
    rep(i, 26) que.push(P(C[i], i));
    string ans = "";
    rep(i, N) {
        while(true) {
            P t = que.top();
            que.pop();
            if(i == 0) {
                t.first--;
                ans = ans + (char)(t.second + 'a');
                que.push(t);
                break;
            } else if(i == 1) {
                if(t.second + 'a' == ans[i - 1]) {
                    tmp.push(t);
                    continue;
                }
            } else {
                if(t.second + 'a' == ans[i - 1] || t.second + 'a' == ans[i - 2]) {
                    tmp.push(t);
                    continue;
                }
            }
            ans = ans + (char)(t.second + 'a');
            t.first--;
            que.push(t);
            while(!tmp.empty()) {
                que.push(tmp.top());
                tmp.pop();
            }
            break;
        }
    }
    string t = ans;
    sort(all(S));
    sort(all(t));
    if(S == t)
        cout << ans << endl;
    else
        cout << -1 << endl;
}
accuracy: 1
time_ms: 380
memory_kb: 3592
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tuplis = array<ll, 3>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll LINF=0x1fffffffffffffff;
const ll MINF=0x7fffffffffff;
const int INF=0x3fffffff;
const int MOD=1000000007;
const int MODD=998244353;
const ld DINF=numeric_limits<ld>::infinity();
const ld EPS=1e-9;
const ld PI=3.1415926535897932;
const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
#define overload4(_1,_2,_3,_4,name,...) name
#define overload3(_1,_2,_3,name,...) name
#define rep1(n) for(ll i=0;i<n;++i)
#define rep2(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=a;i<b;++i)
#define rep4(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i=n;i--;)
#define rrep2(i,n) for(ll i=n;i--;)
#define rrep3(i,a,b) for(ll i=b;i-->(a);)
#define rrep4(i,a,b,c) for(ll i=(a)+((b)-(a)-1)/(c)*(c);i>=(a);i-=c)
#define rrep(...) overload4(__VA_ARGS__,rrep4,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define each1(i,a) for(auto&&i:a)
#define each2(x,y,a) for(auto&&[x,y]:a)
#define each3(x,y,z,a) for(auto&&[x,y,z]:a)
#define each(...) overload4(__VA_ARGS__,each3,each2,each1)(__VA_ARGS__)
#define all1(i) begin(i),end(i)
#define all2(i,a) begin(i),begin(i)+a
#define all3(i,a,b) begin(i)+a,begin(i)+b
#define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__)
#define rall1(i) (i).rbegin(),(i).rend()
#define rall2(i,k) (i).rbegin(),(i).rbegin()+k
#define rall3(i,a,b) (i).rbegin()+a,(i).rbegin()+b
#define rall(...) overload3(__VA_ARGS__,rall3,rall2,rall1)(__VA_ARGS__)
#define sum(...) accumulate(all(__VA_ARGS__),0LL)
#define dsum(...) accumulate(all(__VA_ARGS__),0.0L)
#define Msum(...) accumulate(all(__VA_ARGS__),0_M)
#define elif else if
#define unless(a) if(!(a))
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define Sort(a) sort(all(a))
#define Rev(a) reverse(all(a))
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
template<class T> auto min(const T& a){ return *min_element(all(a)); }
template<class T> auto max(const T& a){ return *max_element(all(a)); }
inline ll popcnt(ull a){ return __builtin_popcountll(a); }
ll gcd(ll a, ll b){ while(b){ ll c = b; b = a % b; a = c; } return a; }
ll lcm(ll a, ll b){ if(!a || !b) return 0; return a * b / gcd(a, b); }
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }
vector<ll> iota(ll n){ vector<ll> a(n); iota(a.begin(), a.end(), 0); return a; }
vector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
map<ll,ll> factor_map(ull x){ map<ll,ll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans[i] = 1; while((x /= i) % i == 0) ans[i]++; } if(x != 1) ans[x] = 1; return ans; }
vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
template<class T> unordered_map<T, ll> press(vector<T> a){ Uniq(a); unordered_map<T, ll> ans; rep(a.size()) ans[a[i]] = i; return ans; }
template<class T> map<T, ll> press_map(vector<T> a){ Uniq(a); map<T, ll> ans; rep(a.size()) ans[a[i]] = i; return ans; }
int scan(){ return getchar(); }
void scan(int& a){ scanf("%d", &a); }
void scan(unsigned& a){ scanf("%u", &a); }
void scan(long& a){ scanf("%ld", &a); }
void scan(long long& a){ scanf("%lld", &a); }
void scan(unsigned long long& a){ scanf("%llu", &a); }
void scan(char& a){ do{ a = getchar(); }while(a == ' ' || a == '\n'); }
void scan(float& a){ scanf("%f", &a); }
void scan(double& a){ scanf("%lf", &a); }
void scan(long double& a){ scanf("%Lf", &a); }
void scan(vector<bool>& a){ for(unsigned i = 0; i < a.size(); i++){ int b; scan(b); a[i] = b; } }
void scan(char a[]){ scanf("%s", a); }
void scan(string& a){ cin >> a; }
template<class T> void scan(vector<T>&);
template<class T, size_t size> void scan(array<T, size>&);
template<class T, class L> void scan(pair<T, L>&);
template<class T, size_t size> void scan(T(&)[size]);
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
template<class T> void scan(deque<T>& a){ for(auto&& i : a) scan(i); }
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i); }
template<class T, class L> void scan(pair<T, L>& p){ scan(p.first); scan(p.second); }
template<class T, size_t size> void scan(T (&a)[size]){ for(auto&& i : a) scan(i); }
template<class T> void scan(T& a){ cin >> a; }
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){ scan(head); in(tail...); }
void print(){ putchar(' '); }
void print(bool a){ printf("%d", a); }
void print(int a){ printf("%d", a); }
void print(unsigned a){ printf("%u", a); }
void print(long a){ printf("%ld", a); }
void print(long long a){ printf("%lld", a); }
void print(unsigned long long a){ printf("%llu", a); }
void print(char a){ printf("%c", a); }
void print(char a[]){ printf("%s", a); }
void print(const char a[]){ printf("%s", a); }
void print(float a){ printf("%.15f", a); }
void print(double a){ printf("%.15f", a); }
void print(long double a){ printf("%.15Lf", a); }
void print(const string& a){ for(auto&& i : a) print(i); }
template<class T> void print(const complex<T>& a){ if(a.real() >= 0) print('+'); print(a.real()); if(a.imag() >= 0) print('+'); print(a.imag()); print('i'); }
template<class T> void print(const vector<T>&);
template<class T, size_t size> void print(const array<T, size>&);
template<class T, class L> void print(const pair<T, L>& p);
template<class T, size_t size> void print(const T (&)[size]);
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T> void print(const deque<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T, size_t size> void print(const array<T, size>& a){ print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T, class L> void print(const pair<T, L>& p){ print(p.first); putchar(' '); print(p.second); }
template<class T, size_t size> void print(const T (&a)[size]){ print(a[0]); for(auto i = a; ++i != end(a); ){ putchar(' '); print(*i); } }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
#ifdef DEBUG
inline ll __lg(ull __n){ return sizeof(ull) * __CHAR_BIT__  - 1 - __builtin_clzll(__n); }
#define debug(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...) void(0)
#endif
int first(bool i = true){ return out(i?"first":"second"); }
int First(bool i = true){ return out(i?"First":"Second"); }
int yes(bool i = true){ return out(i?"yes":"no"); }
int Yes(bool i = true){ return out(i?"Yes":"No"); }
int No(){ return out("No"); }
int YES(bool i = true){ return out(i?"YES":"NO"); }
int NO(){ return out("NO"); }
int Yay(bool i = true){ return out(i?"Yay!":":("); }
int possible(bool i = true){ return out(i?"possible":"impossible"); }
int Possible(bool i = true){ return out(i?"Possible":"Impossible"); }
int POSSIBLE(bool i = true){ return out(i?"POSSIBLE":"IMPOSSIBLE"); }
void Case(ll i){ printf("Case #%lld: ", i); }



signed main(){
    LL(n);
    STR(s);
    vec(ll,cnt,26);
    each(i,s)cnt[i-'a']++;
    string ans="--";
    auto p=iota(26);
    sort(all(p),[&](ll a,ll b){return cnt[a]>cnt[b];});
    rep(n){
        const ll x=p[0],y=p[1],z=p[2];
        if(ans.end()[-1]!=x+'a'&&ans.end()[-2]!=x+'a'){
            cnt[x]--;
            ans+=char(x+'a');
        }
        elif(ans.end()[-1]!=y+'a'&&ans.end()[-2]!=y+'a'){
            if(!cnt[y]--)return out(-1);
            ans+=char(y+'a');
        }
        else{
            if(!cnt[z]--)return out(-1);
            ans+=char(z+'a');
        }
        sort(all(p),[&](ll a,ll b){return cnt[a]>cnt[b];});
    }
    out(ans.substr(2));
}
accuracy: 1
time_ms: 10
memory_kb: 3916
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int64 N;
	cin >> N;
	string s;
	cin >> s;
	priority_queue<pair<int, char>> pq;
	int cnt[26] = {};
	REP(i, N) {
	  cnt[s[i] - 'a']++;
	}
  int last[26] = {};
	REP(i, 26) {
	  if (cnt[i])
  	  pq.emplace(cnt[i], (char)(i + 'a'));
	  last[i] = -INF;
	}
	bool ok = 1;
	string res("");
	while (pq.size()) {
	  ok = 0;
	  vector<pair<int, char>> v;
//	  cout << pq.size() << endl;
	  while (pq.size()) {
	    int num;
	    char c;
	    tie(num, c) = pq.top(); pq.pop();
//	    cout << num << " " << c << " " << res.size() << " " << last[c-'a'] << endl;
	    if (res.size() - last[c -'a'] > 1) {
	      ok = 1;
	      res += c;
	      num--;
	      last[c - 'a'] = res.size();
	      if (num > 0)
  	      v.emplace_back(num, c);
	      break;
	    } else {
	      v.emplace_back(num, c);
	    }
	  }
	  if (!ok) break;
	  for (auto& x : v) pq.push(x);
	}
	if (ok) {
	  cout << res << endl;
	} else {
	  cout << -1 << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3416
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a >= b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll n;cin >> n;
    string s;cin >> s;
    vl alp(26);rep(i,n){
        alp[s[i]-'a']++;
    }
    string ans;
    rep(i,n){
        priority_queue<pair<ll,char>,vector<pair<ll,char>>,less<pair<ll,char>>> que;
        rep(j,26){
            que.push({alp[j],'a'+j});
        }
        /*rep(j,26){
            cout << que.top().fi<<" "<< que.top().se <<endl;
            que.pop();
        }
        return 0;*/
        bool ok=false;
        rep(j,26){
            pair<ll,char> p=que.top();que.pop();
            if(i>=1)if(ans[i-1]==p.se)continue;
            if(i>=2)if(ans[i-2]==p.se)continue;
            if(p.fi==0)break;
            //cout << p.se <<endl;
            ans+=p.se;
            //cout << ans <<endl;
            alp[p.se-'a']--;
            ok=true;
            break;
        }
        if(!ok) cout << -1 <<endl,exit(0);
    }
    cout << ans <<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3816
final_rank: 10

code_snippet: // #pragma GCC optimize("unroll-loops", "omit-frame-pointer", "inline")
// #pragma GCC option("arch=native", "tune=native", "no-zero-upper")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("tree-vectorize","openmp","predictive-commoning")
// #pragma GCC option("D_GLIBCXX_PARALLEL","openmp")

// #pragma GCC optimize("O3")
// #pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
// #define int long long
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000               // 2e9
#define LLINF 2000000000000000000ll  // 2e18 (llmax:9e18)
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define dmp(x) cerr << #x << ": " << x << endl;

template <class T>
void chmin(T &a, const T &b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
  if (a < b) a = b;
}

template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.fi << ',' << p.sec;
  return os;
}
template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.fi >> p.sec;
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 < vec.size()) os << ' ';
  }
  return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}
void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

#define endl "\n"

void solve() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> cnt(30);
  for (int i = 0; i < n; i++) { cnt[s[i] - 'a']++; }
  vector<pair<int, int>> occ;
  for (int i = 0; i < 30; i++) { occ.emplace_back(cnt[i], i); }
  sort(all(occ));
  reverse(all(occ));
  // dmp(occ);
  set<int> idx;
  for (int i = 0; i < n; i++) idx.insert(i);
  vector<int> ans(n);
  for (int i = 0; i < 30; i++) {
    int c = occ[i].second;
    int pre = -3;
    for (int j = 0; j < occ[i].first; j++) {
      auto it = idx.lower_bound(pre + 3);
      if (it == idx.end()) {
        cout << -1 << endl;
        return;
      }
      ans[*it] = c;
      pre = *it;
      idx.erase(it);
    }
  }
  for (int i = 0; i < n; i++) { cout << (char)(ans[i] + 'a'); }
  cout << endl;
  return;
}

signed main() {
  fastio();
  solve();
  // int t; cin >> t; while(t--)solve();

  // int t; cin >> t;
  // for(int i=1;i<=t;i++){
  //   cout << "Case #" << i << ": ";
  //   solve();
  // }
  return 0;
}
accuracy: 0.7391304347826086
time_ms: 20
memory_kb: 8084
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }


string solve(int N, const string &S) {
    map<char,int> ma;
    for (auto c : S) ma[c]++;
    if (ma.size() == 1) {
        if (N == 1) return S;
        else return "-1";
    }
    else if (ma.size() == 2) {
        if (N == 2) return S;
        else return "-1";
    }
    else {
        string res = "";
        while (!ma.empty()) {
            int vmax = -1;
            char pmax;
            for (auto it : ma) {
                if (res.size() > 0 && res.back() == it.first) continue;
                if (res.size() > 1 && res[res.size() - 2] == it.first) continue;
                if (chmax(vmax, it.second)) pmax = it.first;
            }
            if (vmax == -1) return "-1";
            res += pmax;
            ma[pmax]--;
            if (ma[pmax] == 0) ma.erase(pmax);
        }
        return res;
    }
}

int main() {
    int N;
    string S;
    cin >> N >> S;
    cout << solve(N, S) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3496
final_rank: 4