๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.63K subscribers
5.61K photos
3 videos
95 files
10.6K links
๐ŸšฉMain Group - @SuperExams
๐Ÿ“Job Updates - @FresherEarth

๐Ÿ”ฐAuthentic Coding Solutions(with Outputs)
โš ๏ธDaily Job Updates
โš ๏ธHackathon Updates & Solutions

Buy ads: https://telega.io/c/cs_algo
Download Telegram
def solve(priceList):
    floors = len(priceList)
    frequencies = len(priceList[0])
    dp = [[0] * frequencies for _ in range(floors)]
    for j in range(frequencies):
        dp[0][j] = priceList[0][j]
    for i in range(1, floors):
        for j in range(frequencies):
            min_cost = float('inf')
            for k in range(frequencies):
                if k != j:
                    min_cost = min(min_cost, dp[i-1][k])
            dp[i][j] = priceList[i][j] + min_cost
    return min(dp[-1])
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<vector<int>> tree;
vector<int> values;
long long total = 0;

void dfs(int node, int parent, vector<int>& path) {
    path.push_back(values[node]);

    if (path.size() % 2 == 1) {
      
        vector<int> sorted_path = path;
        sort(sorted_path.begin(), sorted_path.end());
        int median = sorted_path[sorted_path.size() / 2];
        total += median;
    }

    for (int neighbor : tree[node]) {
        if (neighbor != parent) {
            dfs(neighbor, node, path);
        }
    }

    path.pop_back();
}

int main() {
    int n;
    cin >> n;

    values.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> values[i];
    }

    tree.resize(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        tree[u].push_back(v);
        tree[v].push_back(u);
    }

    vector<int> path;
    dfs(0, -1, path);

    cout << total<< endl;

    return 0;
}

//median path โœ