Skip to content

Instantly share code, notes, and snippets.

@nattybear
Last active August 18, 2022 11:18
Show Gist options
  • Save nattybear/c24f22f7fe3636234a1a986319a9e82f to your computer and use it in GitHub Desktop.
Save nattybear/c24f22f7fe3636234a1a986319a9e82f to your computer and use it in GitHub Desktop.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] trees = new int[n];
for (int i = 0; i < n; i++)
trees[i] = sc.nextInt();
for (int i = 0; i <= 2000000000; i++)
if (m == cutMany(trees, n, i)) {
System.out.println(i);
break;
}
}
static int cut(int tree, int height) {
int diff = tree - height;
if (diff >= 0)
return diff;
else
return 0;
}
static int cutMany(int[] trees, int n, int height) {
return Arrays.stream(trees)
.map((tree) -> cut(tree, height))
.sum();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment