https://www.acmicpc.net/problem/1248
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int n;
static char[][] signMatrix;
static int[] sequence;
static void input() {
Reader scanner = new Reader();
n = scanner.nextInt();
signMatrix = new char[n + 1][n + 1];
sequence = new int[n + 1];
String info = scanner.nextLine();
int index = 0;
for(int row = 1; row <= n; row++) {
for(int col = row; col <= n; col++) {
signMatrix[row][col] = info.charAt(index);
index++;
}
}
}
static void solution() {
recFunc(1);
}
static void recFunc(int index) {
if(index > n) {
for(int idx = 1; idx <= n; idx++) System.out.print(sequence[idx] + " ");
System.exit(0);
}
for(int num = -10; num <= 10; num++) {
sequence[index] = num;
if(isPossible(index)) recFunc(index + 1);
}
}
static boolean isPossible(int index) {
for(int start = 1; start <= index; start++) {
int sum = 0;
for(int idx = start; idx <= index; idx++) {
sum += sequence[idx];
if(signMatrix[start][idx] != (sum > 0 ? '+' : (sum == 0 ? '0' : '-')))
return false;
}
}
return true;
}
public static void main(String[] args) {
input();
solution();
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}