Created
January 4, 2016 16:14
-
-
Save Jack-Saleem/70928fbe1f4d0aa2d88d to your computer and use it in GitHub Desktop.
Codeforces 574A BearandElections in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Scanner; | |
public class BearAndElections | |
{ | |
public static void main(String[] args) | |
{ | |
Scanner z=new Scanner(System.in); | |
int n=z.nextInt(); | |
int[] a=new int[n]; | |
int[] b=new int[n-1]; | |
boolean k=true; | |
for(int i=0;i<n;i++) | |
a[i]=z.nextInt(); | |
int aa=a[0]; | |
for(int i=0;i<n-1;i++) | |
b[i]=a[i+1]; | |
for(int i=1;i<n;i++){ | |
if(a[0]<a[i] || a[0]==a[i]) | |
k=false; | |
} | |
Arrays.sort(b); | |
if(k) | |
System.out.println(0); | |
else { | |
int kk=0; | |
while(kk!=b.length) { | |
while(a[0]<=b[b.length-1] && !k) { | |
a[0]++; | |
b[b.length-1]--; | |
kk=0; | |
for(int j=0;j<b.length;j++) { | |
if(a[0]>b[j]) | |
kk++; | |
} | |
if(kk==b.length) { | |
k=true;break;} | |
else | |
Arrays.sort(b); | |
} | |
}System.out.println(a[0]-aa); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment