Untitled
unknown
plain_text
a year ago
1.1 kB
10
Indexable
Never
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; class HelloWorld { public static long getMinimumMoves(List<Integer> quantity){ long sum=0l; for(int i:quantity){ sum+=(long)i; } long half = sum/2, s=0l; for(int i=0;i<quantity.size();i++){ s+=(long)quantity.get(i); if(s>half){ if(i==0 || i==quantity.size()-1) break; return s-(sum-s); }else if(s==half){ return 0; } } if(quantity.get(quantity.size()-1)>half){ return 2l*(long)quantity.get(quantity.size()-1)-sum; } return (long)quantity.get(quantity.size()-1)-(sum-(long)quantity.get(0)); } public static void main(String[] args) { List<Integer> quantity = new ArrayList(); quantity.add(3); quantity.add(3); quantity.add(6); quantity.add(3); quantity.add(9); System.out.println(getMinimumMoves(quantity)); } }