/*
 * Solution by Christian Kauth - some long time ago ;)
 * This problem can be solved by Dynamic Programming as it was discussed in the seminar.
 * Recursion is then used to draw the connections - for fun :)
 */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const ok = 1;										// connection cost per match for (w-b) or (b-w)
const no = 1<<20;									// connection cost per match for (w-w) or (b-b)
const int costPerMatch[2][2]={no,ok,ok,no};			// connection cost per match matrix
const INF = 1<<20;									// there will be never more than INF matches to be used

int N;								// number of pearls
int perl[100];						// perl chain
long int totalCost[100][100];		// totalCost[i][j] is the minimum number of matches to be used to interconnect all the perls with positions i to j
int height[100][100];				// height[i][j] is the highest point of positions i to j
int split[100][100];				// position at which the two subtrees were merged
char routing[200][200];				// figure showing the optimal routing

void read_input()
{
	ifstream fin;
	string chain;

	fin.open("GPS.IN");
	fin>>N;
	fin>>chain;
	for (int i=0; i<chain.length(); i++)
		perl[i]=int(chain[i])-48;
}

void initialize()
{
	for (int i=0; i<N; i++)
		for (int j=0; j<N; j++)
			if (i==j+1)
				totalCost[i][j]=0;
			else
				totalCost[i][j]=INF;

	for (int a=0; a<2*N; a++)
		for (int b=0; b<2*N; b++)
			routing[a][b]=' ';
}

int max(int& a, int& b)
{
	if (a>b)
		return a;
	else
		return b;
}

void connect_perls()
{
	long int alternativeCost;
	int j;

	for (int length=2; length<=N; length+=2)
		for (int i=0; i<=N-length; i++)
		{
			j=i+length-1;
			alternativeCost=totalCost[i+1][j-1]+(length-1+(height[i+1][j-1]+1)*2)*costPerMatch[perl[i]][perl[j]];
			if (alternativeCost < totalCost[i][j])
			{
				totalCost[i][j]=alternativeCost;
				height[i][j]=height[i+1][j-1]+1;
				split[i][j]=0;
			}
			for (int cut=i+2; cut<=i+length-2; cut+=2)
			{
				alternativeCost = totalCost[i][cut-1]+totalCost[cut][j];
				if (alternativeCost < totalCost[i][j])
				{
					totalCost[i][j]=alternativeCost;
					height[i][j]=max(height[i][cut-1],height[cut][j]);
					split[i][j]=cut;
				}
			}
		}
}

void output()
{
	ofstream fout;

	fout.open("GPS.OUT");
	fout<<totalCost[0][N-1]<<endl<<endl;

	for (int i=height[0][N-1]; i>=0; i--)
	{
		for (int j=0; j<2*N; j++)
			fout<<routing[i][j];
		fout<<endl;
	}
	for (int j=0; j<N; j++)
		fout<<perl[j]<<" ";
	fout<<endl;
}

void picasso(int i, int j)
{
	if (split[i][j]==0)
	{
		routing[height[i][j]][2*i]='+';
		routing[height[i][j]][2*j]='+';
		for (int k=2*i+1; k<2*j; k++)
			routing[height[i][j]][k]='-';
		for (int l=height[i][j]-1; l>=0; l--)
		{
			routing[l][i*2]='|';
			routing[l][j*2]='|';
		}
		if (i+1<j) picasso(i+1,j-1);
	}
	else
	{
		picasso(i,split[i][j]-1);
		picasso(split[i][j],j);
	}
}

int main()
{
	read_input();
	initialize();
	connect_perls();
	picasso(0,N-1);
	output();
	return 0;
}
