# CFR 613 C. Necklace ( Ad hoc, Palindrome )

Problem - C - Codeforces

N ≤ 26

O( 1e5 )

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

int N;
int C[ 26 ];

signed main(){
ios::sync_with_stdio( 0 );
{
cin >> N;
for( int i = 0; i < N; ++i ){
cin >> C[ i ];
}
}
{
int odd = -1;
for( int i = 0; i < N; ++i ){
if( ~ C[ i ] & 1 ) continue;
if( odd != -1 ){
cout << 0 << endl;
for( int j = 0; j < N; ++j ){
for( int k = 0; k < C[ j ]; ++k ){
cout << ( char ) ( 'a' + j );
}
}
cout << endl;
exit( 0 );
}
odd = i;
}
if( odd == -1 ){
int g = 0;
for( int j = 0; j < N; ++j ){
g = __gcd( g, C[ j ] / 2 );
}
cout << 2 * g << endl;
string s;
for( int j = 0; j < N; ++j ){
for( int k = 0; k < C[ j ] / 2 / g; ++k ){
s += ( char ) ( 'a' + j );
}
}
for( int i = 0; i < 2 * g; ++i ){
cout << s;
reverse( s.begin(), s.end() );
}
cout << endl;
} else{
int g = 0;
for( int j = 0; j < N; ++j ){
g = __gcd( g, C[ j ] );
}
cout << g << endl;
string s, t;
for( int j = 0; j < C[ odd ] / g; ++j ){
t += ( char ) ( 'a' + odd );
}
for( int j = 0; j < N; ++j ){
if( odd == j ) continue;
for( int k = 0; k < C[ j ] / 2 / g; ++k ){
s += ( char ) ( 'a' + j );
}
}
string ns = s + t;
reverse( s.begin(), s.end() );
ns += s;
for( int i = 0; i < g; ++i ){
cout << ns;
}
cout << endl;
}
}
return 0;
}
```