Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- public class Main {
- public static void main(String[] args) {
- // Create a scanner object to read the input
- Scanner scanner = new Scanner(System.in);
- // Read the snake size and number of jumps from the input
- int size = scanner.nextInt();
- int jumps = scanner.nextInt();
- // Read the path of fruits from the input
- ArrayList<Integer> path = new ArrayList<Integer>();
- while (scanner.hasNextInt()) {
- path.add(scanner.nextInt());
- }
- // Close the scanner object
- scanner.close();
- // Initialize the growth balance to zero
- int balance = 0;
- // Loop through the path and update the snake size and growth balance
- for (int i = 0; i < path.size(); i++) {
- // If the fruit is neutral, do nothing
- if (path.get(i) == 0) {
- continue;
- }
- // If the fruit is growing, increase the snake size and decrease the growth balance
- else if (path.get(i) == 1) {
- size++;
- balance--;
- }
- // If the fruit is shrinking, check if there are jumps available
- else if (path.get(i) == -1) {
- // If there are jumps, use one and skip the fruit
- if (jumps > 0) {
- jumps--;
- }
- // If there are no jumps, decrease the snake size and increase the growth balance
- else {
- size--;
- balance++;
- }
- }
- // If the snake size becomes zero or negative, break the loop
- if (size <= 0) {
- break;
- }
- }
- // Print the final snake size and growth balance to the output
- System.out.println(size + " " + balance);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement