import java.util.*;importjava.util.stream.Collectors;classWorkflowNode {
String nodeId;inttimeoutMillis;
ListnextNodes;booleaninitialised;public staticWorkflowNode load(String value) {//Create head node;
Map map = new HashMap<>();
WorkflowNode head= new WorkflowNode("HEAD", 0, null);
map.put(head.nodeId, head);for (String nodeValue : value.split("\\|")) {
String[] properties= nodeValue.split("\\`");
WorkflowNode node= map.get(properties[0]);
node.timeoutMillis= Integer.parseInt(properties[1]);
node.initialised= true;//Check next nodes
if (properties[2].equals("END")) {continue;
}
node.nextNodes= Arrays.stream(properties[2].split(","))
.map(p-> new WorkflowNode(p, 0, null))
.collect(Collectors.toList());
node.nextNodes.forEach(p->map.put(p.nodeId, p));
map.put(node.nodeId, node);
}returnhead;
}public WorkflowNode(String nodeId, int timeoutMillis, ListnextNodes) {this.nodeId =nodeId;this.timeoutMillis =timeoutMillis;this.nextNodes =nextNodes;
}
@OverridepublicString toString() {//TODO Auto-generated method stub
return super.toString();
}
}public classMain
{public static Map map = new HashMap<>();public static void iteratorNode(WorkflowNode node,StacknodeStack){
nodeStack.push(node);
List childNodes =node.nextNodes;if(childNodes==null){
List list = new ArrayList();
Iterator iterator =nodeStack.iterator();while(iterator.hasNext()){
list.add(iterator.next());
}
map.put(node.nodeId, list);return;
}else{
Iterator iterator=childNodes.iterator();while(iterator.hasNext()){
WorkflowNode childNode=iterator.next();
iteratorNode(childNode, nodeStack);
nodeStack.pop();
}
}
}public static intgetPathTime(List list){int sumTime=0;
Iterator iterator =list.iterator();while(iterator.hasNext()){
WorkflowNode node=iterator.next();
sumTime+=node.timeoutMillis;
}returnsumTime;
}public static voidmain(String args[])
{//String value="HEAD`0`A,B,C|A`20`END|B`100`END|C`50`D,E|D`80`F|E`150`END|F`30`END";//System.out.println(Arrays.toString(value.split("\\|")));//[HEAD`0`A,B,C, A`20`END, B`100`END, C`50`D,E, D`80`F, E`150`END, F`30`END]
Scanner cin = newScanner(System.in);while(cin.hasNext())
{
WorkflowNode node=WorkflowNode.load(cin.next());//System.out.println(node.nodeId+node.timeoutMillis+node.nextNodes.get(0).nodeId+node.initialised);
Stack nodeStack=new Stack<>();
iteratorNode(node, nodeStack);//todo 遍历保存路径
int time =Integer.MIN_VALUE;for (Map.Entryentry : map.entrySet()) {
List list=entry.getValue();
time=Math.max(getPathTime(list),time);
}
System.out.println(time);
}
}
}