import java.util.*; class B{ static Scanner s = new Scanner(System.in); public static void main(String[] args) { Product[] products = new Product[Integer.parseInt(s.next())]; for (int i=0;i0;i--) { new Step( products[Integer.parseInt(s.next())-1], Integer.parseInt(s.next()), products[Integer.parseInt(s.next())-1] ); } ArrayDeque queue = new ArrayDeque<>(); queue.add(products[products.length-1]); products[products.length-1].needed++; while(!queue.isEmpty()) { Product p = queue.poll(); if(p.making.isEmpty()) { continue; } for(Step s:p.making) { s.from.needed+=p.needed*s.x; queue.add(s.from); } p.needed=0; } for(int i=0;i making = new ArrayList<>(); } class Step{ Product from,to; long x; Step(Product from, int x, Product to) { this.from=from; this.x=x; to.making.add(this); } }