File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+ class Main {
7+
8+ // IO field
9+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
11+ static StringTokenizer st;
12+
13+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
14+ static int nextInt() {return Integer . parseInt(st. nextToken());}
15+ static long nextLong() {return Long . parseLong(st. nextToken());}
16+ static void bwEnd() throws Exception {bw. flush();bw. close();}
17+
18+ // Additional field
19+
20+ static int N ;
21+ static int [] dp;
22+ static char [] A ;
23+ static final int INF = (int )1e9 ;
24+
25+ public static void main(String [] args) throws Exception {
26+
27+ ready();
28+ solve();
29+
30+ bwEnd();
31+
32+ }
33+
34+ static void ready() throws Exception {
35+
36+ N = Integer . parseInt(br. readLine());
37+ A = br. readLine(). toCharArray();
38+ dp = new int [N ];
39+ Arrays . fill(dp, INF );
40+
41+ }
42+
43+ static void solve() throws Exception {
44+
45+ dp[0 ] = 0 ;
46+ for (int i= 1 ;i< N ;i++ ) {
47+ char tar;
48+ if (A [i] == ' B' ) tar = ' J' ;
49+ else if (A [i] == ' O' ) tar = ' B' ;
50+ else tar = ' O' ;
51+
52+ for (int j= 0 ;j< i;j++ ) if (A [j] == tar) {
53+ dp[i] = Math . min(dp[i], dp[j] + (i- j)* (i- j));
54+ }
55+ }
56+ bw. write((dp[N - 1 ] == INF ? - 1 : dp[N - 1 ]) + " \n " );
57+
58+ }
59+
60+ }
61+
62+ ```
You can’t perform that action at this time.
0 commit comments