Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace acmPC
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = Int32.Parse(Console.ReadLine());
- string[] l = Console.ReadLine().Split(' ');
- Basket[] baskets = new Basket[l.Length];
- for (int i = 0; i < l.Length; i++)
- {
- var s = l[i];
- baskets[i] = new Basket();
- baskets[i].Apples = Int32.Parse(l[i]);
- }
- for (int i = 0; i < n-1; i++)
- {
- l = Console.ReadLine().Split(' ');
- int x = Int32.Parse(l[0]) - 1;
- int y = Int32.Parse(l[1]) - 1;
- baskets[y].AddChild(baskets[x]);
- }
- Console.ReadLine();
- l = Console.ReadLine().Split(' ');
- for (int i = 0; i < l.Length; i++)
- {
- int Q = Int32.Parse(l[i]) - 1;
- Console.Write(baskets[Q].AllApples + " ");
- }
- }
- }
- class Basket
- {
- public int Apples { get; set; }
- private int _allApples = -1;
- public int AllApples
- {
- get
- {
- if (_allApples == -1)
- _allApples = CountApples();
- return _allApples;
- }
- }
- public List<Basket> Children { get; set; }
- public Basket()
- {
- Children = new List<Basket>();
- }
- public void AddChild(Basket child)
- {
- Children.Add(child);
- }
- private int CountApples()
- {
- int p = Apples + Children.Sum(child => child.AllApples);
- return p;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement