|
| 1 | +package community_of_robots; |
| 2 | + |
| 3 | +import java.io.FileReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.PrintWriter; |
| 6 | +import java.util.ArrayDeque; |
| 7 | +import java.util.Scanner; |
| 8 | + |
| 9 | +public class CommunityOfRobots { |
| 10 | + private static final int ROBOT_COUNT_FOR_GROUP_THREE = 5; |
| 11 | + private static final int ROBOT_COUNT_FOR_GROUP_FIVE = 9; |
| 12 | + |
| 13 | + private static long _robotMaxCount(long robotCountStart) { |
| 14 | + long fiveGroup = robotCountStart / 5; |
| 15 | + long threeGroup = (robotCountStart - (fiveGroup * 5)) / 3; |
| 16 | + long result = threeGroup * ROBOT_COUNT_FOR_GROUP_THREE + fiveGroup * ROBOT_COUNT_FOR_GROUP_FIVE; |
| 17 | + |
| 18 | + // максимальное количество групп по 5 |
| 19 | + while (true) { |
| 20 | + boolean threePow = (robotCountStart - fiveGroup * 5 ) % 3 == 0; |
| 21 | + if (threePow) { // если есть группы по 5 и по 3 |
| 22 | + threeGroup = (robotCountStart - fiveGroup * 5) / 3; |
| 23 | + |
| 24 | + long resultTmp = threeGroup * ROBOT_COUNT_FOR_GROUP_THREE + fiveGroup * ROBOT_COUNT_FOR_GROUP_FIVE; |
| 25 | + |
| 26 | + if (result < resultTmp) { |
| 27 | + result = resultTmp; |
| 28 | + } |
| 29 | + |
| 30 | + break; |
| 31 | + } else { |
| 32 | + fiveGroup -= 1; |
| 33 | + } |
| 34 | + |
| 35 | + if (fiveGroup < 0) { |
| 36 | + break; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + threeGroup = robotCountStart / 3; |
| 41 | + |
| 42 | + // максимальное количество групп по 3 |
| 43 | + long resultTmp = threeGroup * ROBOT_COUNT_FOR_GROUP_THREE; |
| 44 | + if (result < resultTmp) { |
| 45 | + result = resultTmp; |
| 46 | + } |
| 47 | + |
| 48 | + return result; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public static void main(String[] args) throws IOException { |
| 53 | + String[] data = new Scanner(new FileReader("input.txt")).nextLine().split(" "); |
| 54 | + long k = Long.parseLong(data[0]); |
| 55 | + int n = Integer.parseInt(data[1]); |
| 56 | + |
| 57 | + long robotCount = k; |
| 58 | + |
| 59 | + ArrayDeque<Long> process = new ArrayDeque<>(); |
| 60 | + process.addLast(k); |
| 61 | + for (int i = 1; i <= n; i++) { |
| 62 | + // удаление роботов с 3го года |
| 63 | + if (i > 3) { |
| 64 | + robotCount -= process.removeFirst(); |
| 65 | + } |
| 66 | + |
| 67 | + // последний год |
| 68 | + if (i == n) { |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + long count = _robotMaxCount(robotCount); |
| 73 | + robotCount += count; |
| 74 | + process.addLast(count); |
| 75 | + } |
| 76 | + |
| 77 | + PrintWriter out = new PrintWriter(System.out); |
| 78 | + out.println(robotCount); |
| 79 | + out.flush(); |
| 80 | + } |
| 81 | +} |
0 commit comments