Skip to content

Instantly share code, notes, and snippets.

@PreSoichiSumi
Created April 26, 2016 08:06
Show Gist options
  • Save PreSoichiSumi/5434890f8b40b3c58c694af16ca8d4a5 to your computer and use it in GitHub Desktop.
Save PreSoichiSumi/5434890f8b40b3c58c694af16ca8d4a5 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static String S;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
S=sc.nextLine();
int[] happiness=new int[S.length()];
int now=0;
for(int i=S.length()-1;i>=0;i--){
if(S.charAt(i)=='+'){
now++;
}
if(S.charAt(i)=='-'){
now--;
}
happiness[i]=now;
}
List<Integer> Ms=new ArrayList<Integer>();
for(int i=0;i<S.length();i++){
if(S.charAt(i)=='M')
Ms.add(happiness[i]);
}
Collections.sort(Ms);
long left=0;
long right=0;
for(int i=0;i<Ms.size();i++){
if(i<(Ms.size()/2)){
left+=Ms.get(i);
}else {
right+=Ms.get(i);
}
}
System.out.println(right-left);
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment