Files
2019-03-15 13:47:54 +04:00

75 lines
1.4 KiB
Java

package chelper;
import io.InputReader;
import io.OutputWriter;
import misc.SimpleSavingChelperSolution;
public class INVENTRY extends SimpleSavingChelperSolution {
public void solve(int testNumber, InputReader in, OutputWriter out) {
wrapSolve(testNumber, in, out);
}
@Override
public void solve(int testNumber) {
int n = in.nextInt();
int[] spaces = new int[n + 2];
int spn = 0;
char[] chars = in.nextString().toCharArray();
int t = 0;
for (int i = 0; i < n; i++) {
if (chars[i] == '#') {
spaces[spn++] = t;
t = 0;
} else {
t++;
}
}
spaces[spn++] = t;
int res = 0;
for (int i = 0; i < spn - 1; ) {
if (spaces[i] == 0) {
i++;
continue;
}
if (spaces[i + 1] >= 1) {
res += spaces[i];
spaces[i + 1] += spaces[i];
spaces[i] = 0;
continue;
}
// spaces[i + 1] == 0
int need = 0;
for (int j = i + 1; j < spn; j++) {
if (spaces[j] == 0) {
spaces[j] = 1;
need++;
}
if (spaces[j] > 1) {
int available = spaces[j] - 1;
int take = Math.min(need, available);
spaces[j] -= take;
need -= take;
if (need == 0) {
break;
}
}
res += need;
}
if (need != 0) {
out.println(-1);
return;
}
}
out.println(res);
}
}