Last active
August 18, 2022 11:18
-
-
Save nattybear/c24f22f7fe3636234a1a986319a9e82f to your computer and use it in GitHub Desktop.
This file contains 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.*; | |
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