Loading...
Submission
Id When Author Problem Language CPU Memory Verdict
17897 2024-04-24 04:19:11 dnt Bonus Project Java 106 ms 40400 kb Runtime Error - 1
Test Cases
CPU Memory Verdict
1 106 ms 40400 kb Runtime Error
2 - - Skipped
3 - - Skipped
4 - - Skipped
5 - - Skipped
Source Code
program.java
Download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. static class Employee {
  5. int id;
  6. long accountBalance;
  7. List<Employee> subordinates;
  8.  
  9. public Employee(int id, long accountBalance) {
  10. this.id = id;
  11. this.accountBalance = accountBalance;
  12. this.subordinates = new ArrayList<>();
  13. }
  14. }
  15.  
  16. static void distributeProjects(Employee leader, int projectLeader, int bonusAmount) {
  17. if (leader.id == projectLeader) {
  18. leader.accountBalance += 2 * bonusAmount;
  19. } else {
  20. leader.accountBalance += bonusAmount;
  21. }
  22.  
  23. for (Employee subordinate : leader.subordinates) {
  24. distributeProjects(subordinate, projectLeader, bonusAmount);
  25. }
  26. }
  27.  
  28. static void propagateBonus(Employee leader) {
  29. long bonus = leader.accountBalance / 2;
  30. for (Employee subordinate : leader.subordinates) {
  31. subordinate.accountBalance += bonus;
  32. propagateBonus(subordinate);
  33. }
  34. }
  35.  
  36. public static void main(String[] args) {
  37. Scanner scanner = new Scanner(System.in);
  38. int t = scanner.nextInt(); // Number of test cases
  39.  
  40. while (t-- > 0) {
  41. int n = scanner.nextInt(); // Number of employees
  42. int q = scanner.nextInt(); // Number of projects
  43.  
  44. Employee[] employees = new Employee[n + 1];
  45. for (int i = 1; i <= n; i++) {
  46. long initialBalance = scanner.nextLong();
  47. employees[i] = new Employee(i, initialBalance);
  48. }
  49.  
  50. for (int i = 0; i < n - 1; i++) {
  51. int a = scanner.nextInt();
  52. int b = scanner.nextInt();
  53. employees[a].subordinates.add(employees[b]);
  54. }
  55.  
  56. for (int i = 0; i < q; i++) {
  57. int projectLeader = scanner.nextInt();
  58. int bonusAmount = scanner.nextInt();
  59. distributeProjects(employees[projectLeader], projectLeader, bonusAmount);
  60. }
  61.  
  62. propagateBonus(employees[1]);
  63.  
  64. for (int i = 1; i <= n; i++) {
  65. System.out.print(employees[i].accountBalance + " ");
  66. }
  67. System.out.println();
  68. }
  69. }
  70. }
  71.