Problem:
Score : 1200 points

Problem Statement

In Takahashi's mind, there is always an integer sequence of length 2 \times 10^9 + 1: A = (A_{-10^9}, A_{-10^9 + 1}, ..., A_{10^9 - 1}, A_{10^9}) and an integer P.

Initially, all the elements in the sequence A in Takahashi's mind are 0, and the value of the integer P is 0.

When Takahashi eats symbols +, -, > and <, the sequence A and the integer P will change as follows:

When he eats +, the value of A_P increases by 1;

When he eats -, the value of A_P decreases by 1;

When he eats >, the value of P increases by 1;

When he eats <, the value of P decreases by 1.

Takahashi has a string S of length N. Each character in S is one of the symbols +, -, > and <.
He chose a pair of integers (i, j) such that 1 \leq i \leq j \leq N and ate the symbols that are the i-th, (i+1)-th, ..., j-th characters in S, in this order.
We heard that, after he finished eating, the sequence A became the same as if he had eaten all the symbols in S from first to last.
How many such possible pairs (i, j) are there?

Constraints

1 \leq N \leq 250000

|S| = N

Each character in S is +, -, > or <.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the answer.

Sample Input 1

5
+>+<-

Sample Output 1

3

If Takahashi eats all the symbols in S, A_1 = 1 and all other elements would be 0.
The pairs (i, j) that leads to the same sequence A are as follows:

(1, 5)

(2, 3)

(2, 4)

Sample Input 2

5
+>+-<

Sample Output 2

5

Note that the value of P may be different from the value when Takahashi eats all the symbols in S.

Sample Input 3

48
-+><<><><><>>>+-<<>->>><<><<-+<>><+<<>+><-+->><<

Sample Output 3

475

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.BitSet;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;


/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author kessido
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        BitSet currentGroup;
        BitSet currentOtherGroup;
        int maxMinSize = -1;
        ArrayList<Integer> options;
        int n;
        BitSet[] edges;
        long start;
        PrintWriter out;
        int p = 0;
        int pMax = 10;

        boolean stateIsValid() {
            BitSet COG = new BitSet(n);
            COG.or(currentOtherGroup);
            int card = COG.cardinality();
            for (int i = 0; i < n; i++) {
                if (COG.get(i)) {
                    COG.and(edges[i]);
                    if (COG.cardinality() != card) return false;
                    COG.or(currentOtherGroup);
                }
            }
            return true;
        }

        void findOne() {
            if (stateIsValid()) {
                int card = currentGroup.cardinality();
                maxMinSize = Math.max(maxMinSize, Math.min(card, n - card));
                p++;
                if (pMax < p) return;
            }
            for (int i1 = 0; i1 < n && p < pMax && checkTime(); i1++) {
                int i = options.get(i1);
                if (currentOtherGroup.get(i)) {
                    for (int j1 = i1 + 1; j1 < n && p < pMax; j1++) {
                        int j = options.get(j1);
                        if (currentOtherGroup.get(j)) {
                            if (!edges[i].get(j)) {
                                BitSet x = (BitSet) edges[i].clone();
                                x.and(currentGroup);
                                boolean r1 = tryToContinue(i, x);
                                x = (BitSet) edges[j].clone();
                                x.and(currentGroup);
                                r1 |= tryToContinue(j, x);
                                if(!r1) return;
                            }
                        }
                    }
                }
            }
        }

        boolean tryToContinue(int j, BitSet x) {
            if (x.cardinality() == currentGroup.cardinality()) {
                currentOtherGroup.flip(j);
                currentGroup.flip(j);
                findOne();
                currentOtherGroup.flip(j);
                currentGroup.flip(j);
                return true;
            }
            return false;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            start = System.nanoTime();
            n = in.NextInt();
            int m = in.NextInt();
            edges = new BitSet[n];
            for (int i = 0; i < n; i++) {
                edges[i] = new BitSet(n);
                edges[i].set(i, true);
            }
            for (int i = 0; i < m; i++) {
                int u = in.NextInt() - 1;
                int v = in.NextInt() - 1;
                edges[u].set(v, true);
                edges[v].set(u, true);
            }

            currentGroup = new BitSet(n);
            currentOtherGroup = new BitSet(n);
            currentOtherGroup.set(0, n, true);
            options = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                options.add(i);
            }
            this.out = out;
            while (checkTime()) {
                Collections.shuffle(options);
                p = 0;
                findOne();
            }
            if (maxMinSize == -1) {
                out.println(-1);
            } else {
                out.println(maxMinSize * (maxMinSize - 1) / 2 + (n - maxMinSize) * (n - maxMinSize - 1) / 2);
            }
        }

        boolean checkTime() {
            return (System.nanoTime() - start < 1_800_000_000L);
        }

    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int NextInt() {
            return Integer.parseInt(next());
        }

    }
}