#include <bits/stdc++.h>
#define N 200020
#define ll long long
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
	while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return f?x:-x;
}
int head[N], nxt[N<<1], to[N<<1], cnt;
void insert(int x, int y) {
	to[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt;
	to[++cnt] = x; nxt[cnt] = head[y]; head[y] = cnt;
}
int a[N], b[N], n = read(), k = read();
int f[N], sz[N], nt[N], sum, ans;
int dfs1(int x, int fa) {
	for (int i = head[x]; i; i = nxt[i]) {
		if (to[i] == fa) continue;
		dfs1(to[i], x);
		sz[x] += sz[to[i]];
		nt[x] += nt[to[i]];
	}
}
void dfs2(int x, int fa) {
	// printf("%d %d \n", x, fa);
	f[x] = 1;
	int mx = 0, sx = 0;
	for (int i = head[x]; i; i = nxt[i]) {
		if (to[i] == fa) continue;
		dfs2(to[i], x);
		if (!nt[to[i]]) f[x] += sz[to[i]];
		else if (!b[to[i]])
			if (f[to[i]] > mx) sx = mx, mx = f[to[i]];
			else if (f[to[i]] > sx) sx = f[to[i]];
	}
	f[x] += mx;
	if (!b[x]) {
		if (nt[x] == sum) ans = max(ans, f[x] + sx + n - sz[x]);
		else ans = max(ans, f[x] + sx);
	}
}
bool check(int mid) {
	// printf("%d:\n", mid);
	ans = sum = 0;
	for (int i = 1; i <= n; i++) {
		sz[i] = 1;
		sum += nt[i] = b[i] = a[i] <= mid;
	}
	dfs1(1, 0);
	dfs2(1, 0);
	// puts(ans >= k ? "Yes" : "No");
	return ans >= k;
}
int main(int argc, char const *argv[]) {
	int l = 1, r = 0, ans;
	for (int i = 1; i <= n; i++)
		r = max(r, a[i] = read());
	for (int i = 1; i < n; i++)
		insert(read(), read());
	r ++;
	while (l <= r) {
		int mid = l + r >> 1;
		if (check(mid)) l = mid + 1;
		else ans = mid, r = mid - 1;
	}
	printf("%d\n", ans);
	return 0;
}
	 	  		  	       			 	 				//hellobig_tq//hello//hello


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 2e5 + 5;

struct edge
{
    int to, next;

    edge(int to = 0, int next = 0) : to(to), next(next) {}
} G[maxn << 1];
int head[maxn], tot, pre[maxn], a[maxn], dp[maxn][2], sz[maxn], n, k, m, que[maxn];

void init()
{
    memset(head, -1, sizeof(head));
    tot = 0;
}

void addedge(int u, int v)
{
    G[tot] = edge(v, head[u]);
    head[u] = tot++;
    G[tot] = edge(u, head[v]);
    head[v] = tot++;
}

bool dfs(int u, int fa)
{
    bool ret = a[u] >= m;
    dp[u][0] = dp[u][1] = -1;
    sz[u] = 0;
    pre[u] = fa;
    for(int i = head[u]; ~i; i = G[i].next)
    {
        int v = G[i].to;
        if(v == fa) continue;
        bool tmp = dfs(v, u);
        ret &= tmp;
        if(a[u] >= m)
        {
            if(tmp) sz[u] += sz[v] + 1;
            else if(dp[u][0] <= dp[v][0] + sz[v] + 1)
            {
                dp[u][1] = dp[u][0];
                dp[u][0] = dp[v][0] + sz[v] + 1;
            }
            else if(dp[u][1] < dp[v][0] + sz[v] + 1) dp[u][1] = dp[v][0] + sz[v] + 1;
        }
    }
    return ret;
}

bool bfs(int s)
{
    int front = 0, rear = 0;
    que[rear++] = s;
    while(front < rear)
    {
        int u = que[front++], fa = pre[u];
        if(a[u] >= m && max(dp[u][0], 0) + sz[u] + 1 >= k) return true;
        for(int i = head[u]; ~i; i = G[i].next)
        {
            int v = G[i].to;
            if(v == fa) continue;
            que[rear++] = v;
            if(a[u] >= m && a[v] >= m)
            {
                if(dp[u][1] == -1 && ~dp[v][0]) sz[v] += sz[u] + 1;
                else
                {
                    int tmp1 = dp[u][0] == dp[v][0] + sz[v] + 1 ? dp[u][1] : dp[u][0];
                    int tmp2 = ~dp[v][0] ? sz[u] + 1 : sz[u] - sz[v];
                    if(dp[v][0] <= tmp1 + tmp2)
                    {
                        dp[v][1] = dp[v][0];
                        dp[v][0] = tmp1 + tmp2;
                    }
                    else if(dp[v][1] < tmp1 + tmp2) dp[v][1] = tmp1 + tmp2;
                }
            }
        }
    }
    return false;
}

int main()
{
    while(~scanf("%d%d", &n, &k))
    {
        init();
        int _max = 0;
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d", &a[i]);
            _max = max(_max, a[i]);
        }
        for(int i = 1; i < n; ++i)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            addedge(u, v);
        }
        int l = 1, r = _max;
        while(l < r)
        {
            m = (l + r + 1) >> 1;
//            if(m > 3)
//            {
//                cout << 1 << endl;
//            }
            dfs(1, 0);
            bfs(1) ? l = m : r = m - 1;
        }
        printf("%d\n", l);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef NeverBeRed
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
#define F first
#define S second

int main()
{
	#ifdef TurnRed
		freopen("a.in", "r", stdin);
		//freopen("a.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(0), cin.tie(0);

	int n, k;
	cin >> n >> k;
	vector<int> val(n);
	for (auto &i : val) cin >> i;
	vector<vector<int>> adj(n);
	for (int i = 1, u, v; i < n; ++i)
	{
		cin >> u >> v;
		--u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	auto solve = [&](int mn)
	{
		auto w = [&](int u) { return val[u] >= mn; };

		vector<int> sz(n), dp(n), wsz(n), rdp(n);
		vector<multiset<int>> sub(n);
		for (int i = 0; i < n; ++i)
			sub[i].insert(0);

		function<void(int, int)> dfs = [&](int u, int p)
		{
			sz[u] = 1;
			wsz[u] = w(u);
			for (auto v : adj[u])
				if (v != p)
				{
					dfs(v, u);
					sz[u] += sz[v];
					wsz[u] += wsz[v];
					if (sz[v] == wsz[v])
						dp[u] += sz[v];
					else
						sub[u].insert(rdp[v]);
				}
			rdp[u] = (dp[u] + 1 + *sub[u].rbegin()) * w(u);
		};
		dfs(0, -1);

		int best = 0;
		function<void(int, int)> dfs2 = [&](int u, int p)
		{
//			debug(u, rdp[u]);
			best = max(best, rdp[u]);
			for (auto v : adj[u])
				if (v != p)
				{
					int rdpu = rdp[u], rdpv = rdp[v];

					int dpu = dp[u], u_dpv = -1;
					if (sz[v] == wsz[v])
						dp[u] -= sz[v];
					else
						sub[u].erase(sub[u].find(rdp[v])), u_dpv = rdp[v];
					int szu = sz[u], wszu = wsz[u];
					sz[u] -= sz[v];
					wsz[u] -= wsz[v];

					rdp[u] = (dp[u] + 1 + *sub[u].rbegin()) * w(u);

					int dpv = dp[v], szv = sz[v], wszv = wsz[v];
					sz[v] += sz[u];
					wsz[v] += wsz[u];
					int v_dpu = -1;
					if (sz[u] == wsz[u])
						dp[v] += sz[u];
					else
						sub[v].insert(rdp[u]), v_dpu = rdp[u];
					rdp[v] = (dp[v] + 1 + *sub[v].rbegin()) * w(v);

					dfs2(v, u);

					if (v_dpu != -1) sub[v].erase(sub[v].find(v_dpu));
					sz[v] = szv;
					wsz[v] = wszv;
					dp[v] = dpv;

					wsz[u] = wszu;
					sz[u] = szu;
					if (u_dpv != -1) sub[u].insert(u_dpv);
					dp[u] = dpu;

					rdp[u] = rdpu;
					rdp[v] = rdpv;
				}
		};
		dfs2(0, -1);

		return best;
	};

//	debug(solve(5));

	int lo = 1, hi = 1e6;
	while (lo < hi)
	{
		int md = (lo + hi + 1) >> 1;
		if (solve(md) >= k)
			lo = md;
		else
			hi = md - 1;
	}
	cout << lo << "\n";

	return 0;
}


#include <cmath>
#include <map>
#include<fstream>
#include<algorithm>
#include <iostream>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include <iomanip>
#include<assert.h>
#include<memory.h>
#include<bitset>
#include <cassert>
#include <stack>
#include <ctime>
#include <queue>
#include <vector>
#include <iterator>
#include <math.h>
#include <unordered_map>
#include<cstring>
#include <cmath>
#include <sstream>
#include<cstring>
#include<cstdlib>
#include <set>
#include <cstdio>
#include<map>
using namespace std;
#define fast {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
#define ll  long long
#define EPS (1e-8);
#define clr(x) memset(x,0,sizeof(x));
#define sz(x) ((int)(x.size()))
#define REP(i,v) for(int i = 0 ; i < sz(v) ; ++i)
#define neg(x) memset(x,-1,sizeof(x));
#define read(file) freopen (file,"r",stdin)
#define write(file) freopen (file,"w",stdout)
#define INF (1LL << 28)
#define endl "\n"
#define M(x,y) make_pair(x,y)
typedef vector<int> vi;
typedef pair<int,int> ii;
inline int toInt(string s){int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
#define MAXN 200005
vector<int> g[MAXN];
int val[MAXN];
int n,k;
bool bad[MAXN];
int DP[MAXN];
int deg[MAXN];
int ben[MAXN];
queue<int> Q;
int maxr = 0;

int solve(int u,int p)
{

	DP[u] = ben[u];
	int c1 = -1;
	int c2 = -1;
	REP(j,g[u])
	{
		int v = g[u][j];
		if(v == p) continue;
		DP[u] = max(DP[u],solve(v,u) + ben[u]);
		if(DP[v] <= c1)
		{
			if(DP[v] > c2) c2 = DP[v];
		}
		else
		{
			c2 = c1;
			c1 = DP[v];
		}

	}
	maxr = max(maxr,DP[u]);
	maxr = max(maxr,ben[u] + c1 + c2);
	return DP[u];
}
bool used[MAXN];
bool can(int x)
{
	maxr = 0;
	clr(bad);
	clr(used);
	for(int i = 0 ; i < n ; ++i)
	{
		deg[i] = g[i].size();
		if(val[i] < x)
		{
			bad[i] = 1;
			ben[i] = -INF;
		}
		else
			ben[i] = 1;
		if(!bad[i] && deg[i] == 1) Q.push(i);
	}

	while(!Q.empty())
	{
		int u = Q.front(); Q.pop();
		//deg[u]--;
		//cerr << u+1 << endl;
		used[u] = 1;
		REP(j,g[u])
		{
			int v = g[u][j];
			if(!bad[v])
			{
				if(!used[v])
				{
					ben[v] += ben[u];
					ben[u] = 0;
					deg[v]--;
					if(deg[v] == 1)
						Q.push(v);
				}

			}
		}
	}
	//for(int i = 0 ; i < n ; ++i)
	//cerr << i << " " << ben[i] << endl;
	memset(DP,-1,sizeof(DP));
	solve(0,-1);
	return maxr >= k;
}
int main()
{
	scanf("%d %d",&n,&k);

	int mini = 1 << 30;
	for(int i = 0 ; i < n ; ++i)
	{
		scanf("%d",&val[i]);
		mini = min(mini,val[i]);
	}

	int u,v;
	for(int j = 0 ; j < n - 1 ; ++j)
	{
		scanf("%d %d",&u,&v);
		--u; --v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	int lo = 0;
	int hi = 1 << 29;
	int ans = -1;

	while(lo <= hi)
	{
		int mid = lo + (hi - lo) / 2;
		if(can(mid))
		{
			ans = mid;
			lo = mid + 1;

		}
		else hi = mid - 1;
	}
	printf("%d",ans);
	puts("");

}


#include <functional>
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <assert.h>
#include <bitset>
#include <string>
#include <cstdio>
#include <math.h>
#include <complex>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <set>
#include <bitset>
#include <map>
using namespace std;

const int N = 200001;

int n, k;
vector < int > G[N];
int a[N];
int dp1[N], dp2[N];
int sum[N];
int cnt[N];
pair < int, int > pr[N];

void dfs1(int v, int p, int x)
{
	cnt[v] = 1;
	pr[v] = make_pair(-1, -1);
	for (int i = 0; i < G[v].size(); ++i)
	{
		int to = G[v][i];
		if (to == p)
			continue;
		dfs1(to, v, x);
		cnt[v] += cnt[to];
		if (cnt[to] == dp1[to])
			dp1[v] += dp1[to];
		else
		{
			if (pr[v].first == -1 || dp1[pr[v].first] < dp1[to])
			{
				pr[v].second = pr[v].first;
				pr[v].first = to;
			}
			else if (pr[v].second == -1 || dp1[pr[v].second] < dp1[to])
				pr[v].second = to;
		}
	}
	if (a[v] < x)
		dp1[v] = sum[v] = 0;
	else
	{
		sum[v] = dp1[v];
		dp1[v] += 1;
		if (pr[v].first != -1)
			dp1[v] += dp1[pr[v].first];
	}
}

void dfs2(int v, int p, int x)
{
	if (p == -1 || a[p] < x)
	{
		dp2[v] = 0;
	}
	else
	{
		int c = sum[p] + 1;
		int mx = 0;
		if (pr[p].first != -1 && pr[p].first != v)
			mx = dp1[pr[p].first];
		else if (pr[p].second != -1 && pr[p].second != v)
			mx = dp1[pr[p].second];
		if (dp1[v] == cnt[v])
			c -= dp1[v];
		dp2[v] = c;
		if (dp2[p] == n - cnt[p])
			dp2[v] += dp2[p];
		else
			mx = max(mx, dp2[p]);
		dp2[v] += mx;
	}
	for (int i = 0; i < G[v].size(); ++i)
	{
		int to = G[v][i];
		if (to == p)
			continue;
		dfs2(to, v, x);
	}
}

bool check(int x)
{
	for (int i = 0; i < n; ++i)
		dp1[i] = dp2[i] = sum[i] = 0;
	dfs1(0, -1, x);
	dfs2(0, -1, x);
	for (int i = 0; i < n; ++i)
	{
		if (a[i] < x)
			continue;
		int ans = sum[i] + 1;
		int mx = 0;
		if (pr[i].first != -1)
			mx = dp1[pr[i].first];
		if (dp2[i] == n - cnt[i])
			ans += dp2[i];
		else
			mx = max(mx, dp2[i]);
		if (ans + mx >= k)
			return true;
	}
	return false;
}
int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int i = 1; i < n; ++i)
	{
		int u, v; scanf("%d%d", &u, &v); u--; v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	int l = 0, r = 1000001;
	while (l + 1 < r)
	{
		int mid = (l + r) / 2;
		if (check(mid))
			l = mid;
		else
			r = mid;
	}
	printf("%d\n", l);
    return 0;
}


#include <bits/stdc++.h>

const int N = 2e5 + 5;
struct DP {
	int sum, mx;
	DP operator + (const DP &rhs) const {
		return DP {sum + rhs.sum, std::max (mx, rhs.mx)};
	}
};
int a[N], total[N], down[N], dp[N];
bool good[N];
std::vector<int> edge[N];
int n, k;

void DFS(int u, int fa)	{
	total[u] = 1;
	int sum = 0, mx = 0;
	for (auto v: edge[u])	{
		if (v == fa)	continue;
		DFS (v, u);
		total[u] += total[v];
		if (down[v] == total[v]) {
			sum += total[v];
		} else {
			mx = std::max (mx, down[v]);
		}
	}
	down[u] = sum + mx + 1;
	if (!good[u])	down[u] = 0;
}

void DFS2(int u, int fa, int up)	{
	std::vector<int> children;
	int sum = 0, mx = 0;
	for (auto v: edge[u])	{
		if (v == fa)	continue;
		if (down[v] == total[v]) {
			sum += down[v];
		} else {
			mx = std::max (mx, down[v]);
		}
		children.push_back (v);
	}
	if (up == n - total[u]) {
		sum += up;
	} else {
		mx = std::max (mx, up);
	}
	dp[u] = sum + mx + 1;
	if (!good[u])	dp[u] = 0;

	int sz = children.size ();
	if (sz == 0)	return ;
	std::vector<DP> predown (sz + 1);
	predown[0] = {0, 0};
	for (int i=0; i<sz; ++i)	{
		int v = children[i];
		predown[i+1] = predown[i];
		if (down[v] == total[v]) {
			predown[i+1].sum += down[v];
		} else {
			predown[i+1].mx = std::max (predown[i+1].mx, down[v]);
		}
	}
	std::vector<DP> sufdown (sz + 1);
	sufdown[sz] = {0, 0};
	for (int i=sz-1; i>=0; --i)	{
		int v = children[i];
		sufdown[i] = sufdown[i+1];
		if (down[v] == total[v]) {
			sufdown[i].sum += down[v];
		} else {
			sufdown[i].mx = std::max (sufdown[i].mx, down[v]);
		}
	}
	for (int i=0; i<sz; ++i) {
		int v = children[i];
		DP now = predown[i] + sufdown[i+1];
		if (up == n - total[u]) {
			now.sum += up;
		} else {
			now.mx = std::max (now.mx, up);
		}
		int new_up = now.sum + now.mx + 1;
		if (!good[u]) new_up = 0;
		DFS2 (v, u, new_up);
	}
}

bool check(int v)	{
	for (int i=1; i<=n; ++i)	good[i] = (a[i] >= v);
	DFS (1, 0);
	DFS2 (1, 0, 0);
	for (int i=1; i<=n; ++i)	{
		if (dp[i] >= k)	return true;
	}
	return false;
}

int main()	{
	scanf ("%d%d", &n, &k);
	for (int i=1; i<=n; ++i)	{
		scanf ("%d", a+i);
	}
	for (int u, v, i=1; i<n; ++i)	{
		scanf ("%d%d", &u, &v);
		edge[u].push_back (v);
		edge[v].push_back (u);
	}
	int low = 0, high = 1000005;
	while (low < high)	{
		int mid = (low + high + 1) >> 1;
		if (check (mid))	{
			low = mid;
		} else {
			high = mid - 1;
		}
	}
	printf ("%d\n", low);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef NeverBeRed
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
#define F first
#define S second

int main()
{
	#ifdef TurnRed
		//freopen("a.in", "r", stdin);
		//freopen("a.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(0), cin.tie(0);

	int n, k;
	cin >> n >> k;
	vector<int> val(n);
	for (auto &i : val) cin >> i;
	vector<vector<int>> adj(n);
	for (int i = 1, u, v; i < n; ++i)
	{
		cin >> u >> v;
		--u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	auto solve = [&](int mn)
	{
		auto w = [&](int u) { return val[u] >= mn; };

		vector<int> sz(n), dp(n), wsz(n), rdp(n);
		vector<multiset<int>> sub(n);
		for (int i = 0; i < n; ++i)
			sub[i].insert(0);

		function<void(int, int)> dfs = [&](int u, int p)
		{
			sz[u] = 1;
			wsz[u] = w(u);
			for (auto v : adj[u])
				if (v != p)
				{
					dfs(v, u);
					sz[u] += sz[v];
					wsz[u] += wsz[v];
					if (sz[v] == wsz[v])
						dp[u] += sz[v];
					else
						sub[u].insert(rdp[v]);
				}
			rdp[u] = (dp[u] + 1 + *sub[u].rbegin()) * w(u);
		};
		dfs(0, -1);

		int best = 0;
		function<void(int, int)> dfs2 = [&](int u, int p)
		{
			best = max(best, rdp[u]);
			for (auto v : adj[u])
				if (v != p)
				{
					int rdpu = rdp[u], rdpv = rdp[v];

					//int dpu = dp[u], u_dpv = -1;
					if (sz[v] == wsz[v])
						dp[u] -= sz[v];
					else
						sub[u].erase(sub[u].find(rdp[v]));//, u_dpv = rdp[v];
					//int szu = sz[u], wszu = wsz[u];
					sz[u] -= sz[v];
					wsz[u] -= wsz[v];

					rdp[u] = (dp[u] + 1 + *sub[u].rbegin()) * w(u);

					//int dpv = dp[v], szv = sz[v], wszv = wsz[v];
					sz[v] += sz[u];
					wsz[v] += wsz[u];
					//int v_dpu = -1;
					if (sz[u] == wsz[u])
						dp[v] += sz[u];
					else
						sub[v].insert(rdp[u]);//, v_dpu = rdp[u];
					rdp[v] = (dp[v] + 1 + *sub[v].rbegin()) * w(v);

					dfs2(v, u);

					rdp[v] = rdpv;
					if (sz[u] == wsz[u])
						dp[v] -= sz[u];
					else
						sub[v].erase(sub[v].find(rdp[u]));

					wsz[v] -= wsz[u];
					sz[v] -= sz[u];

					rdp[u] = rdpu;

					wsz[u] += wsz[v];
					sz[u] += sz[v];

					if (sz[v] == wsz[v])
						dp[u] += sz[v];
					else
						sub[u].insert(rdp[v]);

					//rdp[u] = rdpu;
					//rdp[v] = rdpv;
				}
		};
		dfs2(0, -1);

		return best;
	};

	int lo = 1, hi = 1e6;
	while (lo < hi)
	{
		int md = (lo + hi + 1) >> 1;
		if (solve(md) >= k)
			lo = md;
		else
			hi = md - 1;
	}
	cout << lo << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int n,sz[N],a[N],c,ret,dp[N];
pair<int,int> m[N][2];
vector<int> adj[N];
void dfs(int x,int p){
	int f=0,u,i,d;dp[x]=0;
	if(!sz[x])f=1,++sz[x];
	d= a[x]>=c ? 1 :0;
	for(i=0;i<adj[x].size();++i){
		u= adj[x][i];
		if(u!=p){
			dfs(u,x);
			if(f)sz[x]+=sz[u];
			if(dp[u]==sz[u])dp[x]+=dp[u];
			else{
				if(m[x][0].first<=dp[u])
					m[x][1]= m[x][0],m[x][0]=make_pair(dp[u],u);
				else if(m[x][1].first<dp[u])
					m[x][1]= make_pair(dp[u],u);
			}
		}
	}
	dp[x]+=d+ m[x][0].first;
	if(d==0)dp[x]=0;
}
int ar[3];
void dfs2(int x,int p,int d){
	int v=0,u,i,t=d,nd;
	if(a[x]>=c){
		if(d==n-sz[x])v=d,t=0;
		ar[0]= m[x][0].first,ar[1]= m[x][1].first,ar[2]=t;
		sort(ar,ar+3);
		ret= max(ret, dp[x]+ ar[2]+ar[1]+v-m[x][0].first);
	}
	for(i=0;i<adj[x].size();++i){
		u= adj[x][i];
		if(u!=p){
			nd= dp[x]-m[x][0].first+ max(m[x][0].first,t)+v;
			if(u==m[x][0].second)
				nd= dp[x]-m[x][0].first+ max(m[x][1].first,t)+v;
			else if(sz[u]==dp[u])
				nd-= sz[u];
			if(a[x]<c)nd=0;
			dfs2(u,x,nd);
		}
	}
}
int main(){
	int i,x,y,q,l,r,k,mid;
	cin>>n>>k;
	for(i=1;i<=n;++i)scanf("%d",&a[i]);
	for(i=0;i<n-1;++i)scanf("%d%d",&x,&y),adj[x].push_back(y),adj[y].push_back(x);
	dfs(1,0);
	l=1,r= 1e6;
	while(l<r){
		mid= (l+r+1)/2;
		c= mid;
		memset(m,0,sizeof(m));ret=0;
		dfs(1,0);
		dfs2(1,0,0);
		if(ret>=k)l= mid;
		else
			r= mid-1;
	}
	printf("%d\n",l);
}


#include <bits/stdc++.h>
#define N 200020
#define ll long long
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
	while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return f?x:-x;
}
int head[N], nxt[N<<1], to[N<<1], cnt;
void insert(int x, int y) {
	to[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt;
	to[++cnt] = x; nxt[cnt] = head[y]; head[y] = cnt;
}
int a[N], b[N], n = read(), k = read();
int f[N], sz[N], nt[N], sum, ans;
int dfs1(int x, int fa) {
	for (int i = head[x]; i; i = nxt[i]) {
		if (to[i] == fa) continue;
		dfs1(to[i], x);
		sz[x] += sz[to[i]];
		nt[x] += nt[to[i]];
	}
}
void dfs2(int x, int fa) {
	// printf("%d %d \n", x, fa);
	f[x] = 1;
	int mx = 0, sx = 0;
	for (int i = head[x]; i; i = nxt[i]) {
		if (to[i] == fa) continue;
		dfs2(to[i], x);
		if (!nt[to[i]]) f[x] += sz[to[i]];
		else if (!b[to[i]])
			if (f[to[i]] > mx) sx = mx, mx = f[to[i]];
			else if (f[to[i]] > sx) sx = f[to[i]];
	}
	f[x] += mx;
	if (!b[x]) {
		if (nt[x] == sum) ans = max(ans, f[x] + sx + n - sz[x]);
		else ans = max(ans, f[x] + sx);
	}
}
bool check(int mid) {
	// printf("%d:\n", mid);
	ans = sum = 0;
	for (int i = 1; i <= n; i++) {
		sz[i] = 1;
		sum += nt[i] = b[i] = a[i] <= mid;
	}
	dfs1(1, 0);
	dfs2(1, 0);
	// puts(ans >= k ? "Yes" : "No");
	return ans >= k;
}
int main(int argc, char const *argv[]) {
	int l = 1, r = 0, ans;
	for (int i = 1; i <= n; i++)
		r = max(r, a[i] = read());
	for (int i = 1; i < n; i++)
		insert(read(), read());
	r ++;
	while (l <= r) {
		int mid = l + r >> 1;
		if (check(mid)) l = mid + 1;
		else ans = mid, r = mid - 1;
	}
	printf("%d\n", ans);
	return 0;
}
	 	  		  	       			 	 				//hellobig_tq


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<double,double> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const double PI = 4*atan(1);
const ll INF = 4e18;
const int MX = 100001;

int x,k,n,q,c[9];
vi ok;

template<int SZ> struct mat {
    array<array<ll,SZ>,SZ> d;

    mat() {
        F0R(i,SZ) F0R(j,SZ) d[i][j] = INF;
    }

    mat operator+(const mat& m) {
        mat<SZ> a;
        F0R(i,SZ) F0R(j,SZ) a.d[i][j] = (d[i][j]+m.d[i][j]) % MOD;
        return a;
    }

    mat operator*(const mat& m) {
        mat<SZ> a;
        for (int i: ok)
            for (int j: ok)
                for (int k: ok)
                    a.d[i][k] = min(a.d[i][k],d[i][j]+m.d[j][k]);
        return a;
    }

    mat operator^(ll p) {
        mat<SZ> a, b(*this);
        F0R(i,SZ) a.d[i][i] = 0;

        while (p) {
            if (p&1) a = a*b;
            b = b*b;
            p /= 2;
        }

        return a;
    }

    void print() {
        F0R(i,SZ) {
            F0R(j,SZ) cout << d[i][j] << " ";
            cout << "\n";
        }
        cout << "------------\n";
    }
};

vl mult(vl a, mat<256> b) {
    vl A(256); F0R(i,256) A[i] = INF;
    for (int i: ok)
        for (int j: ok)
            A[j] = min(A[j],a[i]+b.d[i][j]);

    return A;
}

mat<256> trans;
vl a(256);
set<int> man;
map<int,int> wei;
int lst = 1;

void solve(int ind) {
    a = mult(a,trans^(ind-lst));

    mat<256> TRANS = trans;
    F0R(i,1<<k) if (__builtin_popcount(i) == x) {
        if (i&1) {
            F0R(j,k) if (!((i/2)&(1<<j))) {
                if (wei.count(ind+j+1)) {
                    TRANS.d[i][(1<<j)+i/2] = c[j+1]+wei[ind+j+1];
                } else TRANS.d[i][(1<<j)+i/2] = c[j+1];
            }
        } else {
            TRANS.d[i][i/2] = 0;
        }
    }

    a = mult(a,TRANS);
    lst = ind+1;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> x >> k >> n >> q;
    FOR(i,1,k+1) cin >> c[i];
    F0R(i,1<<k) if (__builtin_popcount(i) == x) ok.pb(i);
    for (int i: ok) {
        if (i&1) {
            F0R(j,k) if (!((i/2)&(1<<j))) trans.d[i][(1<<j)+i/2] = c[j+1];
        } else {
            trans.d[i][i/2] = 0;
        }
    }
    if (n > x) man.insert(n-x);

    F0R(i,q) {
        int p,wp; cin >> p >> wp;
        wei[p] = wp;
        FOR(j,1,k+1) if (p-j >= 1 && p-j <= n-x) man.insert(p-j);
    }

    F0R(i,256) a[i] = INF;
    a[(1<<x)-1] = 0;
    for (int i: man) solve(i);
    cout << a[(1<<x)-1];

}

// read the question correctly (is y a vowel?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)


#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int MN = 75;

struct mat {
    ll T[MN][MN];
    void O() {
        for(int i = 0; i < MN; i++) for(int j = 0; j < MN; j++) T[i][j] = 1e18;
    }
    void I() {
        for(int i = 0; i < MN; i++) for(int j = 0; j < MN; j++) T[i][j] = i == j;
    }
};

mat mult(mat a, mat b) {
    mat ret;
    ret.O();

    for(int i = 0; i < MN; i++) {
        for(int j = 0; j < MN; j++) {
            for(int k = 0; k < MN; k++) {
                ret.T[i][j] = min(ret.T[i][j], a.T[i][k] + b.T[k][j]);
            }
        }
    }

    return ret;
}
mat exp(mat &x, int n) {
    if(n == 1) return x;
    mat tmp = exp(x, n / 2);
    if(n % 2) return mult(mult(tmp, tmp), x);
    else return mult(tmp, tmp);
}

int X, K, N, Q;
int C[10];
pii W[30];
map<int, int> chk;

vector<int> Mask;
int inv[256];
mat P, Y, A;

int main() {
    scanf("%d %d %d %d", &X, &K, &N, &Q);

    for(int i = 1; i <= K; i++) {
        scanf("%d", &C[i]);
    }

    for(int i = 0; i < Q; i++) {
        scanf("%d %d", &W[i].first, &W[i].second);
        W[i].first--;

        chk[ W[i].first ] = W[i].second;
    }

    sort(W, W + Q);

    for(int i = 0; i < (1 << K); i++) {
        int cnt = 0;
        for(int j = 0; j < K; j++) if(i & (1 << j)) cnt++;

        if(cnt == X) Mask.push_back(i);
    }
    for(int i = 0; i < Mask.size(); i++) inv[ Mask[i] ] = i;

    A.O();
    A.T[ inv[ ((1 << X) - 1) ] ][0] = 0;
    P.O();

    for(int i = 0; i < Mask.size(); i++) {
        int mask = Mask[i];
        if(!(mask & 1)) {
            P.T[i][ inv[mask >> 1] ] = 0;
            continue;
        }

        for(int j = 1; j <= K; j++) {
            if(mask & (1 << j)) continue;

            int mask2 = mask ^ (1 << j);
            mask2 >>= 1;

            P.T[i][ inv[mask2] ] = C[j];
        }
    }

    int cur_len = X;

    //*
    for(int q = Q - 1; q >= 0; q--) {
        int len = N - W[q].first;

        if(len > cur_len) {
            A = mult(exp(P, len - cur_len), A);
            cur_len = len;
        }

        while(cur_len < N - W[q].first + K && cur_len < N) {
            int p = N - cur_len - 1;

            Y.O();

            for(int i = 0; i < Mask.size(); i++) {
                int mask = Mask[i];
                if(!(mask & 1)) {
                    Y.T[i][ inv[mask >> 1] ] = 0;
                    continue;
                }

                for(int j = 1; j <= K; j++) {
                    if(mask & (1 << j)) continue;

                    int mask2 = mask ^ (1 << j);
                    mask2 >>= 1;

                    Y.T[i][ inv[mask2] ] = C[j] + (chk.find(p + j) != chk.end()? chk[p + j] : 0);
                }
            }

            A = mult(Y, A);
            cur_len++;
        }
    }
    //*/

    if(N > cur_len) {
        A = mult(exp(P, N - cur_len), A);
    }

    printf("%lld", A.T[ inv[(1 << X) - 1] ][0]);
}


#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<time.h>
#include<math.h>
#include<memory>
#include<vector>
#include<bitset>
#include<fstream>
#include<stdio.h>
#include<utility>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int c[15];
int ma[305];
int cnt=0;
const long long inf=1999999999999999999ll;
struct matrix
{
    long long a[75][75];
    friend matrix operator * (const matrix &a,const matrix &b)
    {
        int i,j,k;
        matrix c;
        for (i=0;i<cnt;i++)
        {
            for (j=0;j<cnt;j++)
            {
                c.a[i][j]=inf;
            }
        }
        for (i=0;i<cnt;i++)
        {
            for (j=0;j<cnt;j++)
            {
                for (k=0;k<cnt;k++)
                {
                    c.a[i][k]=min(c.a[i][k],a.a[i][j]+b.a[j][k]);
                }
            }
        }
        return c;
    }
    matrix ()
    {
        int i;
        int j;
        for (i=0;i<75;i++)
        {
            for (j=0;j<75;j++)
            {
                a[i][j]=inf;
            }
            a[i][i]=0;
        }
    }
};
matrix a;
matrix mul(int x)
{
    if (x==0) return matrix();
    matrix b=mul(x/2);
    b=b*b;
    if (x%2==1) b=b*a;
    return b;
}
pair<int,int> p[75];
int main()
{
    #ifdef absi2011
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int x,k;
    int n,q;
    scanf("%d%d%d%d",&x,&k,&n,&q);
    int i;
    for (i=0;i<k;i++)
    {
        scanf("%d",&c[i]);
    }
    memset(ma,-1,sizeof(ma));
    for (i=0;i<(1<<k);i++)
    {
        int j;
        int sum=0;
        for (j=0;j<k;j++)
        {
            if ((1<<j)&i) sum++;
        }
        if (sum==x) ma[i]=cnt++;
    }
    int j;
    for (i=0;i<cnt;i++)
    {
        for (j=0;j<cnt;j++)
        {
            a.a[i][j]=inf;
        }
    }
    for (i=0;i<(1<<k);i++)
    {
        if (ma[i]==-1) continue;
        if (i&1)
        {
            //Can move!
            for (j=1;j<=k;j++)
            {
                if ((1<<j)&i) continue;
                a.a[ma[i]][ma[((1<<j)^i)>>1]]=c[j-1];
            }
        }
        else
        {
            a.a[ma[i]][ma[i>>1]]=0;
        }
    }
    int now=1;
    for (i=0;i<q;i++)
    {
        scanf("%d%d",&p[i].first,&p[i].second);
    }
    sort(p,p+q);
    matrix xx;
    long long ans=0;
    for (i=0;i<q;i++)
    {
        if (p[i].first>=n-x+1)
        {
            ans+=p[i].second;
            continue;
        }
        xx=xx*mul(p[i].first-now);
        now=p[i].first;
        int j;
        for (j=1;j<(1<<k);j+=2)
        {
            if (ma[j]!=-1)
            {
                int l;
                for (l=0;l<cnt;l++)
                {
                    xx.a[l][ma[j]]+=p[i].second;
                }
            }
        }
    }
    xx=xx*mul(n-x+1-now);
    cout<<xx.a[0][0]+ans<<endl;
    return 0;
}


#include<map>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxs 75
#define maxn 15
typedef long long ll;

int m,k,n,q,cnt;
int state[maxs],inv[maxs*10],p[maxs];
int v[maxn];

map<int,int> h;

inline int read(){
	int x=0,f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x*f;
}

void dfs(int x,int mask,int bitcnt){
	if (x>n){
		if (bitcnt!=m) return;
		state[++cnt]=mask,inv[mask]=cnt;
		return;
	}
	if (bitcnt<m) dfs(x+1,mask|(1<<(x-1)),bitcnt+1);
	dfs(x+1,mask,bitcnt);
}

struct matrix{
	ll a[maxs][maxs];
	void clear(){memset(a,63,sizeof(a));}
	void init(){clear(); for (int i=1;i<=cnt;i++) a[i][i]=0;}
	matrix operator *(const matrix &x)const{
		matrix ret; ret.clear();
		for (int i=1;i<=cnt;i++)
			for (int j=1;j<=cnt;j++)
				for (int k=1;k<=cnt;k++)
					ret.a[i][j]=min(ret.a[i][j],a[i][k]+x.a[k][j]);
		return ret;
	}
	void build(){
		clear();
		for (int i=1;i<=cnt;i++){
			int mask=state[i]; if (~mask&1) a[i][inv[mask>>1]]=0;
			int x=__builtin_ctz(mask)+1; mask^=mask&-mask; mask>>=1;
			for (int j=x+1;j<=n+1;j++)
				if (~mask>>(j-2)&1) a[i][inv[1<<(j-2)|mask]]=v[j-x];
		}
	}
	void print(){
		for (int i=1;i<=cnt;i++){
			for (int j=1;j<=cnt;j++)
				printf("%lld ",a[i][j]);
			puts("");
		}
	}
}ans,trans;

matrix power(matrix a,int k){
	matrix ret; ret.init();
	for (;k;k>>=1,a=a*a) if (k&1) ret=ret*a;
	return ret;
}

matrix brute(matrix a,int pos){
	matrix ret; ret.clear();
	for (int i=1;i<=cnt;i++){
		int mask=state[i]; if (~mask&1) ret.a[1][inv[mask>>1]]=min(ret.a[1][inv[mask>>1]],a.a[1][i]);
		int x=__builtin_ctz(mask)+1; mask^=mask&-mask,mask>>=1;
		for (int j=x+1;j<=n+1;j++)
			if (~mask>>(j-2)&1){
				ll res=a.a[1][i]+v[j-x]+h[pos+j-1];
				int tmp=1<<(j-2)|mask;
				ret.a[1][inv[tmp]]=min(ret.a[1][inv[tmp]],res);
			}
	}
	return ret;
}

int main(){
	m=read(),n=read(),k=read()-m+1,q=read();
	for (int i=1;i<=n;i++) v[i]=read();
	dfs(1,0,0); trans.build();
	ans.clear(),ans.a[1][1]=0;
	int pre=1;
	for (int i=1,x,y;i<=q;i++) x=read(),y=read(),h[x]=y,p[i]=x;
	sort(p+1,p+q+1); ll res=0;
	while (q&&p[q]>=k) res+=h[p[q]],h[p[q]]=0,--q;
	/*
	for (int i=1;i<=q;i++){
		p=pps[i].ff,w=pps[i].ss;
		if (p>=k){res+=w; continue;}
		ans=ans*power(trans,p-n-pre);
		for (int i=p-n;i<p;i++) ans=brute(ans,i);
		pre=p;
		}*/
	if (p[1]-n-1>=0) ans=ans*power(trans,p[1]-n-1),pre=p[1]-n;
	for (int l=1,r=1;l<=q;l=r+1){
		for (r=l;r<=q&&p[r]-n-pre<=0;++r) pre=p[r]; --r;
		for (int i=max(1,p[l]-n);i<p[r];i++) ans=brute(ans,i);
		if (r+1<=q) ans=ans*power(trans,p[r+1]-n-pre),pre=p[r+1]-n;
	}
	ans=ans*power(trans,k-pre);
	//	for (int i=1;i<k;i++) ans=brute(ans,i);
	printf("%lld\n",ans.a[1][1]+res);
	return 0;
}


#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
#include<algorithm>
#include<bitset>
#include<map>
#include<memory>
#include<list>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<utility>
#include<vector>

using namespace std;

int c[15];
int ma[305];
int cnt = 0;
const long long inf = 1999999999999999999ll;
struct matrix{
    long long a[75][75];
    friend matrix operator * (const matrix &a, const matrix &b){
        matrix c;
        for(int i = 0; i < cnt; i++){
            for (int j = 0; j < cnt; j++){
                c.a[i][j] = inf;
            }
        }
        for(int i = 0; i < cnt; i++){
            for(int j = 0; j < cnt; j++){
                for(int k = 0; k < cnt; k++){
                    c.a[i][k] = min(c.a[i][k],a.a[i][j]+b.a[j][k]);
                }
            }
        }
        return c;
    }
    matrix (){
        for(int i = 0; i < 75; i++){
            for(int j = 0; j < 75; j++){
                a[i][j] = inf;
            }
            a[i][i] = 0;
        }
    }
};

matrix a;
matrix mul(int x){
    if(x == 0){
        return matrix();
    }
    matrix b = mul(x/2);
    b = b*b;
    if(x % 2 == 1){
        b = b*a;
    }
    return b;
}

pair<int, int> p[75];

int main()
{
    int x,k;
    int n,q;
    scanf("%d%d%d%d",&x,&k,&n,&q);
    for(int i = 0; i < k; i++){
        scanf("%d",&c[i]);
    }
    memset(ma, -1, sizeof(ma));
    for (int i = 0; i < (1<<k); i++){
        int sum = 0;
        for(int j = 0; j < k; j++){
            if((1<<j) & i){
                sum++;
            }
        }
        if(sum == x){
            ma[i] = cnt++;
        }
    }
    for(int i = 0; i < cnt; i++){
        for(int j = 0; j < cnt; j++){
            a.a[i][j] = inf;
        }
    }
    for (int i = 0; i < (1<<k); i++){
        if(ma[i] == -1){
            continue;
        }
        if(i&1){
            //Может двигаться
            for(int j = 1; j <= k; j++){
                if((1<<j) & i){
                    continue;
                }
                a.a[ma[i]][ ma[ ((1<<j)^i)>>1 ] ] = c[j-1];
            }
        }
        else{
            a.a[ma[i]][ma[i>>1]] = 0;
        }
    }
    int now = 1;
    for(int i = 0; i < q; i++){
        scanf("%d%d",&p[i].first,&p[i].second);
    }
    sort(p, p+q);
    matrix xx;
    long long ans = 0;
    for(int i = 0; i < q; i++){
        if(p[i].first >= n-x+1){
            ans += p[i].second;
            continue;
        }
        xx = xx*mul(p[i].first-now);
        now = p[i].first;
        for(int j = 1; j < (1<<k); j+=2){
            if(ma[j] != -1){
                for(int l = 0; l < cnt; l++){
                    xx.a[l][ma[j]] += p[i].second;
                }
            }
        }
    }
    xx = xx*mul(n-x+1-now);
    cout << xx.a[0][0]+ans << endl;
    return 0;
}


#include<stdio.h>
#include<algorithm>

using namespace std;

long long sz[(1 << 8)];
long long D[(1 << 8)][(1 << 8)];
long long MD[32][(1 << 8)][(1 << 8)];
long long c[10];
long long mw[(1 << 8)];
long long who[(1 << 8)];
long long who_n = 0;
long long now[(1 << 8)];
long long want[25 * 30+10];
long long made[(1 << 8)];
long long p[55], w[55];

int main() {
	long long x, k, n,q;
	long long i, j,r,o;

	scanf("%lld%lld%lld%lld", &x, &k, &n, &q);
	for (i = 1; i <= k; i++)scanf("%lld", &c[i]);
	for (i = 1; i < (1 << k); i++)sz[i] = sz[i&(i - 1)] + 1;
	for (i = 0; i < (1 << k); i++) {
		for (j = 0; j < (1 << k); j++) {
			D[i][j] = -1;
		}
	}
	for (i = 0; i < (1 << k); i++) {
		if (sz[i] != x)continue;
		who[who_n] = i;
		mw[i] = who_n;
		who_n++;

		long long fo;
		for (j = 0; j < k; j++) {
			if (i&(1<<j)) {
				fo = j;
				break;
			}
		}
		if (fo != 0) {
			D[i][i / 2] = 0;
		}
		for (j = fo + 1; j <= k; j++) {
			if (i&(1 << j))continue;
			long long next = i - (1 << fo) + (1 << j);
			next /= 2;
			D[i][next] = c[j - fo];
		}
	}
	for (i = 0; i < who_n; i++) {
		for (j = 0; j < who_n; j++) {
			MD[0][i][j] = D[who[i]][who[j]];
		}
	}
	for (i = 1; i < 32; i++) {
		for (j = 0; j < who_n; j++) {
			for (r = 0; r < who_n; r++) {
				MD[i][j][r] = -1;
			}
		}
		for (j = 0; j < who_n; j++) {
			for (r = 0; r < who_n; r++) {
				for (o = 0; o < who_n; o++) {
					if (MD[i - 1][j][o] == -1)continue;
					if (MD[i - 1][o][r] == -1)continue;
					if (MD[i][j][r] == -1 || MD[i][j][r] > MD[i - 1][j][o] + MD[i - 1][o][r])
						MD[i][j][r] = MD[i - 1][j][o] + MD[i - 1][o][r];
				}
			}
		}
	}

	long long want_n = 0;
	for (i = 0; i < q; i++) {
		scanf("%lld%lld", &p[i], &w[i]);
		for (j = p[i] - k-1; j <= p[i] + 1; j++) {
			if(j>1)want[want_n++] = j;
		}
	}
	want[want_n++] = n-x+1;
	sort(want, want + want_n);
	want_n = unique(want, want + want_n) - want;

	long long noww = 1;
	for (i = 0; i < who_n; i++)now[i] = made[i] = -1e13;
	now[mw[(1 << x) - 1]] = 0;
	for (long long pp = 0; pp < want_n; pp++) {
		long long next = want[pp];
		if (next > n - x + 1)break;
		if (next - noww == 1) {
			for (i = 0; i < who_n; i++) {
				if (now[i] == -1e13)continue;
				long long hb = who[i];
				long long fp;
				for (j = 0; j < k; j++) {
					if (hb&(1 << j)) {
						fp = j;
						break;
					}
				}
				if (fp > 0) {
					if(made[mw[hb / 2]]==-1e13|| made[mw[hb / 2]]>now[i])
						made[mw[hb / 2]] = now[i];
				}
				for (j = fp + 1; j <= k; j++) {
					if (hb&(1 << j))continue;
					long long nb = hb - (1 << fp) + (1 << j);
					nb /= 2;
					long long pls = c[j - fp];
					for (r = 0; r < q; r++) {
						if (p[r] == noww + j)pls += w[r];
					}
					if (made[mw[nb]] == -1e13 || made[mw[nb]] > now[i] + pls)
						made[mw[nb]] = now[i] + pls;
				}
			}
			for (i = 0; i < who_n; i++) {
				now[i] = made[i];
				made[i] = -1e13;
			}
		}
		else {
			long long delta = next - noww;
			for (i = 0; i < 30; i++) {
				if ((delta&((long long)1 << i)) == 0)continue;
				for (j = 0; j < who_n; j++) {
					if (now[j] == -1e13)continue;
					for (r = 0; r < who_n; r++) {
						if (MD[i][j][r] == -1)continue;
						if (made[r] == -1e13 || made[r] > now[j] + MD[i][j][r])
							made[r] = now[j] + MD[i][j][r];
					}
				}
				for (j = 0; j < who_n; j++) {
					now[j] = made[j];
					made[j] = -1e13;
				}
			}
		}
		noww = want[pp];
	}
	printf("%lld", now[0]);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

struct mat {
    ll T[100][100];
    void O() {
        for(int i = 0; i < 100; i++) for(int j = 0; j < 100; j++) T[i][j] = 1e18;
    }
    void I() {
        for(int i = 0; i < 100; i++) for(int j = 0; j < 100; j++) T[i][j] = i == j;
    }
};

mat mult(mat a, mat b) {
    mat ret;
    ret.O();

    for(int i = 0; i < 100; i++) {
        for(int j = 0; j < 100; j++) {
            for(int k = 0; k < 100; k++) {
                ret.T[i][j] = min(ret.T[i][j], a.T[i][k] + b.T[k][j]);
            }
        }
    }

    return ret;
}
mat exp(mat &x, int n) {
    if(n == 1) return x;
    mat tmp = exp(x, n / 2);
    if(n % 2) return mult(mult(tmp, tmp), x);
    else return mult(tmp, tmp);
}

int X, K, N, Q;
int C[10];
pii W[30];
map<int, int> chk;

vector<int> Mask;
int inv[256];
mat P, Y, A;

int main() {
    scanf("%d %d %d %d", &X, &K, &N, &Q);

    for(int i = 1; i <= K; i++) {
        scanf("%d", &C[i]);
    }

    for(int i = 0; i < Q; i++) {
        scanf("%d %d", &W[i].first, &W[i].second);
        W[i].first--;

        chk[ W[i].first ] = W[i].second;
    }

    sort(W, W + Q);

    for(int i = 0; i < (1 << K); i++) {
        int cnt = 0;
        for(int j = 0; j < K; j++) if(i & (1 << j)) cnt++;

        if(cnt == X) Mask.push_back(i);
    }
    for(int i = 0; i < Mask.size(); i++) inv[ Mask[i] ] = i;

    A.O();
    A.T[ inv[ ((1 << X) - 1) ] ][0] = 0;
    P.O();

    for(int i = 0; i < Mask.size(); i++) {
        int mask = Mask[i];
        if(!(mask & 1)) {
            P.T[i][ inv[mask >> 1] ] = 0;
            continue;
        }

        for(int j = 1; j <= K; j++) {
            if(mask & (1 << j)) continue;

            int mask2 = mask ^ (1 << j);
            mask2 >>= 1;

            P.T[i][ inv[mask2] ] = C[j];
        }
    }

    int cur_len = X;

    //*
    for(int q = Q - 1; q >= 0; q--) {
        int len = N - W[q].first;

        if(len > cur_len) {
            A = mult(exp(P, len - cur_len), A);
            cur_len = len;
        }

        while(cur_len < N - W[q].first + K && cur_len < N) {
            int p = N - cur_len - 1;

            Y.O();

            for(int i = 0; i < Mask.size(); i++) {
                int mask = Mask[i];
                if(!(mask & 1)) {
                    Y.T[i][ inv[mask >> 1] ] = 0;
                    continue;
                }

                for(int j = 1; j <= K; j++) {
                    if(mask & (1 << j)) continue;

                    int mask2 = mask ^ (1 << j);
                    mask2 >>= 1;

                    Y.T[i][ inv[mask2] ] = C[j] + (chk.find(p + j) != chk.end()? chk[p + j] : 0);
                }
            }

            A = mult(Y, A);
            cur_len++;
        }
    }
    //*/

    if(N > cur_len) {
        A = mult(exp(P, N - cur_len), A);
    }

    printf("%lld", A.T[ inv[(1 << X) - 1] ][0]);
}


//
// Created by Admin on 2/1/2018.
//
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll x, n, k, q;
ll c[10];
map<ll, ll> mp, rmp, t;
ll f[110], pre[110];
bool g[100000010];
ll tp = 0;
struct matrix {
    ll x[110][110];
    matrix() {
        memset(x, 0, sizeof(x));
    }
    matrix unit() {
        matrix res;
        for (int i = 0; i < tp; i++) res.x[i][i] = 1;
        return res;
    }
    matrix operator * (matrix A) {
        matrix res;
        for(int i = 0; i < tp; i++){
            for(int j = 0; j < tp; j++) res.x[i][j] = 1e18 + 1;
        }
        for (int i = 0; i < tp; i++) for (int k = 0; k < tp; k++) for (int j = 0; j < tp; j++) {
                    res.x[i][j] = min(res.x[i][j], (x[i][k] + A.x[k][j]));
                }
        return res;
    }
    matrix operator ^ (long long k) {
        if (!k) return unit();
        matrix res, tmp;
        for(int i = 0; i < tp; i++) for (int j = 0; j < tp; j++) {
                res.x[i][j] = tmp.x[i][j] = x[i][j];
            }
        k--;
        while (k) {
            if (k & 1) res = res * tmp;
            tmp = tmp * tmp;
            k >>= 1;
        }
        return res;
    }
} mt;
void nextStep(ll mask){
    if(!(mask & (1 << 0))){
        mt.x[mp[mask]][mp[mask/2]] = -1;
        return;
    }
    for(int i = 1; i <= k; i++){
        if(mask & (1 << i)) continue;
        ll nwmask = mask | (1 << i);
        nwmask /= 2;
        mt.x[mp[mask]][mp[nwmask]] = c[i];
//        cout << mp[mask] << " " << mp[nwmask] << " " << c[i] << "\n";
    }
}
int main(){
    //freopen("../DP/917C.in","r",stdin);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> x >> k >> n >> q;
    for(int i = 1; i <= k; i++) cin >> c[i];
    ll cnt = 0;
    for(int i = 0; i < (1 << k); i++){
        if(__builtin_popcount(i) == x){
            mp[i] = cnt;
            rmp[cnt] = i;
            cnt++;
        }
    }
    ll ans = 1e18;
    for(int i = 0; i < cnt; i++){
        nextStep(rmp[i]);
    }
    tp = cnt;
    for(int i = 0; i < cnt; i++){
        for(int j = 0; j < cnt; j++){
            if(mt.x[i][j] == 0) mt.x[i][j] = 1e18;
            else if(mt.x[i][j] == -1) mt.x[i][j] = 0;
        }
    }
    for(int i = 0; i < cnt; i++){
        if(i != 0) f[i] = 1e18;
    }
    for(int i = 1; i <= q; i++){
        int u, w; cin >> u >> w;
        t[u] = w;
        for(int j = max(0LL, u - k + 1); j <= u; j++) g[j] = 1;
    }
    int tmp = 0;
    for(int i = n - k + 1; i <= n; i++) g[i] = 1;
    for(int i = 2; i <= n - x + 1; i++){
        if(g[i] == 0){
            tmp++;
        } else{
            if(tmp != 0){
                matrix temp = mt ^ tmp;
                tmp = 0;
                for(int j = 0; j < cnt; j++){
                    pre[j] = 1e18;
                    for(int z = 0; z < cnt; z++){
                        pre[j] = min(pre[j], f[z] + temp.x[z][j]);
                    }
                }
                for(int j = 0; j < cnt; j++){
                    f[j] = pre[j];
                }
            }
            for(int j = 0; j < cnt; j++) pre[j] = 1e18;
            for(int j = 0; j < cnt; j++){
                ll mask = rmp[j];
                if((mask & (1 << 0)) == 0){
                    pre[mp[mask / 2]] = min(pre[mp[mask/2]], f[j]);
                    continue;
                }
                for(int z = 1; z <= k; z++){
                    if(mask & (1 << z)) continue;
                    ll nwmask = mask | (1 << z);
                    ll nwpo = i + z - 1;
                    nwmask /= 2;
                    pre[mp[nwmask]] = min(pre[mp[nwmask]], f[j] + c[z] + t[nwpo]);
                }
            }
            for(int j = 0; j < cnt; j++) f[j] = pre[j];
            if(i >= n - k + 1){
                int mask = 0;
                for(int j = n - x + 1; j <= n; j++){
                    int diff = j - i;
                    mask = mask | (1 << diff);
                }
                ans = min(ans, f[mp[mask]]);
            }
        }
    }
    if(1 >= n - k + 1){
        int mask = 0;
        for(int j = n - x + 1; j <= n; j++){
            int diff = j - 1;
            mask = mask | (1 << diff);
        }
        ans = min(ans, f[mp[mask]]);
    }
    cout << ans;
//    cout << f[0] << " " << f[1] << " " << f[2] << "\n";
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <set>
#include <cstdio>
using std::vector;
using std::string;
using std::map;
using std::set;
using std::min;
using std::max;

typedef long long ll;
typedef double dd;

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <set>
#include <cstdio>
using std::vector;
using std::string;
using std::map;
using std::set;
using std::min;
using std::max;

typedef long long ll;
typedef double dd;

constexpr ll magic_mod = 1000000007;
constexpr ll big = 1ll << 60;

typedef ll _et;
struct Matrix
{
    ll size;
    vector< _et > f;
    Matrix(ll size, _et start_value = big) :size(size), f(size * size,start_value) {}
    Matrix(const Matrix& o)  { *this = o;};
    static vector<Matrix> cache;
    inline _et& m(ll a,ll b)
        {
            return f[a*size + b];
        }
    inline const _et& m(ll a,ll b) const
        {
            return f[a*size + b];
        }
    void print()
        {
            for(ll i =0; i<size; ++i)
            {

                for(ll j = 0;  j < size; ++j)
                {
                    std::cout << m(j,i) << " ";
                }
                std::cout << std::endl;
            }

        }

    void operator=(const Matrix& o)
        {
            this->size = o.size;
            this->f = o.f;
        }
    Matrix operator*(const Matrix& o) const
        {
            Matrix res(size);
            for(ll i =0; i<size; ++i)
            {

                for(ll j = 0;  j < size; ++j)
                {
                    for(ll k = 0; k < size; ++k)
                    {
                        res.m(i,j) = min( res.m(i,j), m(k, j) + o.m(i,k));
                    }
                }
            }
            return res;
        }
    Matrix get_pow(ll p)
        {

            Matrix res(*this);
            bool inited = false;
            for(ll i = 1;; ++i)
            {
                if(!p)
                {
                    break;
                }
                if (p % 2)
                {
                    if (!inited)
                    {
                        res = cache[i];
                        inited = true;
                    }
                    else
                    {
                        res = res * cache[i];
                    }

                }
                p /=2;
            }
            return res;
        }
    void fill_cache();
};

const ll xxxx = 40;
vector<Matrix > Matrix::cache;

void Matrix::fill_cache()
{
    cache = vector<Matrix> (xxxx, Matrix(size));
    Matrix res(*this);
    for(ll i = 1; i < xxxx; ++i)
    {
        cache[i] = res;
        res = res * res;
    }
}

vector<ll> standings;
vector<ll> jw;
ll sn;
map<ll,ll> magic_stones;

inline ll btc(ll x)
{
    ll res = 0;
    while(x)
    {
        if (x % 2) ++res;
        x /=2;
    }
    return res;
}

ll get_index(ll standing)
{
    for(ll i = 0; i<standings.size(); ++i)
    {
        if (standings[i] == standing) return i;
    }
    std::cerr << "WTF, INVALID STANDING " << standing << std::endl;
    return -1;
}

void CalcJmpMatrix(Matrix& jumpm, ll pos = -1)
{
    for(ll i = 0; i<standings.size(); ++i)
    {
        ll ss = standings[i];
        ll smallest = 1;
        while( (ss & smallest) == 0) smallest*=2;
        for(ll j = 0; j<jw.size();++j)
        {
            ll ns = ss;
            ns&= ~smallest;
            if (ns & (smallest << j) ) continue;
            ns |= smallest << j;
            if ((ns & 1) == 0 && ns < (1 << jw.size() ))
            {
                auto& x = jumpm.m(i,get_index(ns >> 1));
                ll aw = 0;
                if( pos >= 0 && j!=0)
                {
                    ll pos2 = pos;
                    ll sm2 = smallest >> 1;
                    while ( sm2)
                    {
                        ++pos2;
                        sm2 /= 2;
                    }
                    pos2 +=j;
                    if (magic_stones.find(pos2) != magic_stones.end())
                    {
                        aw = magic_stones[pos2];
                    }
                }
                x = min(x,(ll)jw[j] + aw);
            }
        }
    }
}

vector<ll> mul(const vector<ll>& x, const Matrix& m)
{
    vector<ll> res(sn, big);
    for(ll i = 0; i<sn; ++i)
    {
        for(ll j = 0; j<sn;++j)
        {
            res[i] = min(res[i], x[j] + m.m(j,i));
        }
    }
    return res;
}

void print_state(ll pos, vector<ll>& res)
{
    //std::cout << pos << " res " << res[0] << std::endl;
    // for(auto x : res)
    // {
    //     std::cout << (x != big ? x : -1)<< " ";
    // }
    // std::cout << std::endl;

}

int main()
{
    ll x,k,n,q;
    scanf("%lld%lld%lld%lld",&x,&k,&n,&q);
    jw = vector<ll>(k +1, 0);
    for (ll i = 1; i <=k; ++i)
        scanf("%lld",&(jw[i]) );
    for (ll i = 0; i <q; ++i)
    {
        ll p,pw;
        scanf("%lld%lld",&p,&pw);
        magic_stones[p-1] = pw;
    }
    for(ll i = 0; i < (1 << k); ++i)
    {
        if (btc(i) == x)
        {
            standings.push_back(i);
        }
    }
    sn = standings.size();
    Matrix jumpm(sn);
    CalcJmpMatrix(jumpm);
    jumpm.fill_cache();
    vector<ll> res(sn, big);
    res[0] = 0;
    ll cp = 0;
    ll des_pos = n - x;
    print_state(cp,res);
    for(auto ms : magic_stones)
    {
        if (ms.first == cp) continue;
        if (ms.first - k > cp)
        {
            res = mul(res, jumpm.get_pow(ms.first - k -cp));
            cp = ms.first - k;
            print_state(cp,res);
        }
        for(;cp < ms.first && cp < des_pos;)
        {
            Matrix msm(sn);
            CalcJmpMatrix(msm, cp);
            res = mul(res,msm);
            ++cp;
            print_state(cp,res);
       }
    }
    if (cp < des_pos)
    {
        res = mul(res, jumpm.get_pow(des_pos -cp));
        cp = des_pos;
        print_state(cp,res);
    }
    print_state(cp,res);
    std::cout << res[0] << std::endl;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
int x, k, P, q, B, curx, n;
ll c[10], w;
map<int, ll> mp;
vector<ll> dp, dpn;
vector<int> bitset;
mat a, o, p;
void pow2(mat &a) {
	for (int i : bitset)
		for (int j : bitset)
			p[i][j] = 1e18;
	for (int k : bitset) {
		for (int i : bitset)
			for (int j : bitset)
				p[i][j] = min(p[i][j], a[i][k] + a[k][j]);
	}
	swap(a, p);
}
void operator *=(vector<ll> &dp, mat &a) {
	for (int i : bitset)
		dpn[i] = 1e18;
	for (int i : bitset) {
		for (int j : bitset) {
			dpn[j] = min(dpn[j], dp[i] + a[i][j]);
		}
	}
	swap(dp, dpn);
}
void jump(ll to) {
	a = o;
	ll jump = to - curx;
	for (;jump;pow2(a), jump >>= 1) {
		if (jump & 1) dp *= a;
	}
	curx = to;
}
int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> x >> k >> n >> q;
	B = 1 << k;
	dp.resize(B, 1e18);
	dpn.resize(B, 1e18);
	o.resize(B, vector<ll>(B, 1e18));
	p.resize(B, vector<ll>(B, 1e18));
	for (int b = 1;b < B;b++)
		if (__builtin_popcount(b) == x) bitset.push_back(b);
	dp[bitset[0]] = 0;
	for (int i = 1;i <= k;i++) cin >> c[i];
	for (int cur : bitset) {
		int next = cur >> 1;
		if (cur & 1) {
			for (int i = 1, b = 1;i <= k;i++, b <<= 1) if (~next & b) {
				o[cur][next | b] = c[i];
			}
		}
		else o[cur][next] = 0;
	}
	for (int i = 1;i <= q;i++) {
		cin >> P >> w;
		if (w) mp[P] = w;
	}
	mp[n + k - x + 1] = 0;
	curx = k;
	for (auto it = mp.begin();curx < n + k - x;) {
		if (it->first <= curx - k) {
			it++; continue;
		}
		if (it->first <= curx + 1) {
			for (int i : bitset)
				dpn[i] = 1e18;
			curx++;
			for (int cur : bitset) {
				int next = cur >> 1;
				if (cur & 1) {
					for (int i = 1, b = 1;i <= k;i++, b <<= 1) if (~next & b) {
						dpn[next | b] = min(dpn[next | b], dp[cur] + c[i] + (mp.count(curx - k + i) ? mp[curx - k + i] : 0));
					}
				}
				else dpn[next] = min(dpn[next], dp[cur]);
			}
			swap(dp, dpn);
		}
		else jump(it->first - 1);
	}
	printf("%lld", dp[bitset[0]]);
}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

#define debug(...) printf(__VA_ARGS__), fflush(stdout);

typedef long long LL;
typedef pair<int, int> pii;

const int MAX_STATE = 71;
const LL inf = 1e18;

pii p[30];

int c[10], f[1 << 8];

int x, k, n, q, cnt;

struct matrix {

	LL A[MAX_STATE][MAX_STATE];

	matrix() {
		for (int i = 1; i < MAX_STATE; i++) {
			for (int j = 1; j < MAX_STATE; j++)
				A[i][j] = inf;
			A[i][i] = 0;
		}
	}

	matrix operator * (matrix B) {
		matrix r;
		for (int i = 1; i <= cnt; i++)
			r.A[i][i] = inf;
		for (int i = 1; i <= cnt; i++)
			for (int j = 1; j <= cnt; j++)
				for (int k = 1; k <= cnt; k++)
					r.A[i][j] = min(r.A[i][j], A[i][k] + B.A[k][j]);
		return r;
	}

	matrix operator ^ (int y) {
		matrix x, r;
		for (int i = 1; i <= cnt; i++)
			for (int j = 1; j <= cnt; j++)
				x.A[i][j] = A[i][j];
		if (!y) return matrix();
		while (y) {
			if (y & 1) r = r * x;
			x = x * x; y >>= 1;
		}
		return r;
	}
};

int calc(int x) {
	int sum = 0;
	while (x) {
		if (x & 1) sum++;
		x >>= 1;
	}
	return sum;
}

int main() {
#ifdef xunzhen
	freopen("E.in", "r", stdin);
	freopen("E.out", "w", stdout);
#endif

	cin >> x >> k >> n >> q;
	for (int i = 1; i <= k; i++)
		scanf("%d", &c[i]);
	for (int i = 1; i <= q; i++)
		scanf("%d%d", &p[i].first, &p[i].second);

	for (int i = 0; i < (1 << k); i++)
		if (calc(i) == x) f[i] = ++cnt;

	matrix R;
	for (int i = 1; i <= cnt; i++)
		R.A[i][i] = inf;

	for (int i = 1; i < (1 << k); i++) {
		if (!f[i]) continue;
		if (i & 1) {
			for (int j = 1; j <= k; j++) {
				if ((1 << j) & i) continue;
				R.A[f[i]][f[((1 << j) | i) >> 1]] = c[j];
			}
		}else R.A[f[i]][f[i >> 1]] = 0;
	}

	sort(p + 1, p + q + 1);

	matrix ans;
	LL sum = 0; int now = 1;
	for (int i = 1; i <= q; i++) {
		if (p[i].first > n - x) {
			sum += p[i].second;
			continue;
		}
		ans = ans * (R ^ (p[i].first - now));
		now = p[i].first;
		for (int j = 1; j < (1 << k); j += 2)
			if (f[j])
				for (int k = 1; k <= cnt; k++)
					ans.A[k][f[j]] += p[i].second;
	}
	ans = ans * (R ^ (n - x + 1 - now));

	cout << ans.A[1][1] + sum << endl;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define pq priority_queue
#define MZ 1000000007

int n,kk,xx;
int cnt=0;
int n2b[75],b2n[265];
int bs[10];
long long dis[10];
int p[30];
long long dp[2][75];
map <int,long long> mp;
struct Mat
{
    long long num[75][75];
    friend Mat operator *(Mat x1,Mat x2)
    {
        Mat ans;
        for(int i=1;i<=cnt;i++)
            for(int j=1;j<=cnt;j++)
            {
                ans.num[i][j]=2LL*MZ*MZ;
                for(int k=1;k<=cnt;k++)
                    ans.num[i][j]=min(x1.num[i][k]+x2.num[k][j],ans.num[i][j]);
            }
        return ans;
    }
}A;
queue <int> q;

Mat pows(Mat x,int y)
{
    Mat ans;
    for(int i=1;i<=cnt;i++)
        for(int j=1;j<=cnt;j++)
        {
            if(i==j)
                ans.num[i][j]=0;
            else
                ans.num[i][j]=2LL*MZ*MZ;
        }
    while(y)
    {
        if(y%2)
            ans=ans*x;
        x=x*x;
        y>>=1;
    }
    return ans;
}
void init(int f,int x,int s)
{
    if(f==kk)
    {
        if(x==xx)
        {
            cnt++;
            b2n[s]=cnt;
            n2b[cnt]=s;
        }
        return;
    }
    init(f+1,x+1,s+bs[f]);
    init(f+1,x,s);
}
void solve1(Mat x)
{
    for(int i=1;i<=cnt;i++)
        dp[1][i]=2LL*MZ*MZ;
    for(int i=1;i<=cnt;i++)
        for(int j=1;j<=cnt;j++)
            dp[1][j]=min(dp[0][i]+x.num[i][j],dp[1][j]);
    for(int i=1;i<=cnt;i++)
        dp[0][i]=dp[1][i];
}
void solve2(int y)
{
    for(int i=1;i<=cnt;i++)
        dp[1][i]=2LL*MZ*MZ;
    for(int i=1;i<=cnt;i++)
    {
        if(n2b[i]%2)
        {
            for(int j=1;j<=kk;j++)
            {
                if((n2b[i]&bs[j])==0)
                    dp[1][b2n[(n2b[i]+bs[j])/2]]=min(dp[1][b2n[(n2b[i]+bs[j])/2]],dp[0][i]+A.num[i][b2n[(n2b[i]+bs[j])/2]]+mp[y+j]);
            }
        }
        else
            dp[1][b2n[n2b[i]/2]]=min(dp[1][b2n[n2b[i]/2]],dp[0][i]);
    }
    for(int i=1;i<=cnt;i++)
        dp[0][i]=dp[1][i];
}

int main()
{
    int qq;
    long long w;
    bs[0]=1;
    for(int i=1;i<=8;i++)
        bs[i]=bs[i-1]*2;
    scanf("%d%d%d%d",&xx,&kk,&n,&qq);
    init(0,0,0);
    for(int i=1;i<=kk;i++)
        scanf("%lld",&dis[i]);
    for(int i=1;i<=cnt;i++)
    {
        for(int j=1;j<=cnt;j++)
            A.num[i][j]=2LL*MZ*MZ;
        if(n2b[i]%2)
        {
            for(int j=1;j<=kk;j++)
            {
                if((n2b[i]&bs[j])==0)
                    A.num[i][b2n[(n2b[i]+bs[j])/2]]=dis[j];
            }
        }
        else
            A.num[i][b2n[n2b[i]/2]]=0;
    }
    for(int i=0;i<qq;i++)
    {
        scanf("%d%lld",&p[i],&w);
        mp[p[i]]=w;
    }
    p[qq]=n-xx+1;
    int now=1;
    for(int i=2;i<=cnt;i++)
        dp[0][i]=2LL*MZ*MZ;
    sort(p,p+qq);
    for(int i=0;i<=qq;i++)
    {
        if(p[i]>p[qq])
            p[i]=p[qq];
        if(now+kk<p[i])
        {
            solve1(pows(A,p[i]-kk-now));
            now=p[i]-kk;
        }
        while(now<p[i])
        {
            solve2(now);
            now++;
        }
    }
    printf("%lld\n",dp[0][1]);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int read()
{
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)) { if(c == '-') f = -1; c = getchar(); }
	while(isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); }
	return x * f;
}

typedef long long LL;
const LL inf = 1e18;
int cnt, c[120], f[(1 << 8) + 100];
pair<int, int> q[300];

struct Mat
{
	LL a[100][100];

	Mat operator * (const Mat &A) const
	{
		Mat tmp;
		for(int i = 1; i <= cnt; ++i)
			for(int j = 1; j <= cnt; ++j)
				tmp.a[i][j] = inf;
		for(int i = 1; i <= cnt; ++i)
			for(int j = 1; j <= cnt; ++j)
				for(int k = 1; k <= cnt; ++k)
					tmp.a[i][j] = min(tmp.a[i][j], a[i][k] + A.a[k][j]);
		return tmp;
	}

	Mat operator ^ (int p)
	{
//		int p = P;
//		cout << '*' << p << endl;
		Mat x, r;
		for(int i = 1; i <= cnt; ++i) for(int j = 1; j <= cnt; ++j) if(i != j) r.a[i][j] = inf; else r.a[i][i] = 0;
		for(int i = 1; i <= cnt; ++i)
			for(int j = 1; j <= cnt; ++j)
				x.a[i][j] = a[i][j];
//		cout << '*' << p << endl;
	//	p = 1;
		while(p)
		{
			if(p & 1) r = x * r;
			x = x * x; p >>= 1;
		}
		return r;
	}
};

	/*Mat gao(Mat c, int p)
	{
		cout << '*' << p << endl;
		Mat x, r;
		for(int i = 1; i <= cnt; ++i) for(int j = 1; j <= cnt; ++j) if(i != j) r.a[i][j] = inf;
		for(int i = 1; i <= cnt; ++i)
			for(int j = 1; j <= cnt; ++j)
				x.a[i][j] = c.a[i][j];
//		cout << '*' << p << endl;
	//	p = 1;
		while(p)
		{
			if(p & 1) r = x * r;
			x = x * x; p >>= 1;
		}
		return r;
	}*/
int main()
{
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif

	int x = read(), k = read(), n = read(), Q = read();
	for(int i = 1; i <= k; ++i) c[i] = read();
	for(int i = 1; i <= Q; ++i) q[i].first = read(), q[i].second = read();
	cnt = 0;
	for(int i = 1; i < (1 << k); ++i)
		if(__builtin_popcount(i) == x) f[i] = ++cnt;
	Mat tmp;
	for(int i = 1; i <= cnt; ++i) for(int j = 1; j <= cnt; ++j) tmp.a[i][j] = inf;
	for(int i = 1; i < (1 << k); ++i)
	{
		if(!f[i]) continue;
		if(i & 1)
			for(int j = 1; j <= k; ++j)
			{
				if((1 << j) & i) continue;
				tmp.a[f[((1 << j) | i) >> 1]][f[i]] = c[j];
			}
		else tmp.a[f[i >> 1]][f[i]] = 0;
	}

	sort(q + 1, q + Q + 1);
	Mat ans; for(int i = 1; i <= cnt; ++i) for(int j = 1; j <= cnt; ++j) ans.a[i][j] = inf;
	ans.a[1][1] = 0;
	LL sum = 0; int now = 1;
	for(int i = 1; i <= Q; ++i)
	{
		if(q[i].first > n - x) { sum += q[i].second; continue; }
		ans = (tmp ^ (q[i].first - now)) * ans;
		now = q[i].first;
		for(int j = 1; j < (1 << k); j += 2)
		{
			if(f[j])
				ans.a[f[j]][1] += q[i].second;
		}
//		ans = (tmp ^ (q[i + 1].first - now)) * ans;
//		cout << endl;
//		for(int i = 1; i <= cnt; ++i) cout << ans.a[i][1] << endl;
	}
	ans = (tmp ^ (n - x + 1 - now)) * ans;
/*
	for(int i = 1; i <= cnt; ++i)
	{
		for(int j = 1; j <= cnt; ++j)
			cout << tmp.a[i][j] << ' ';
		cout << endl;
	}
	*/
//	ans = tmp * ans;

	/*
	for(int i = 1; i <= cnt; ++i)
	{
		for(int j = 1; j <= cnt; ++j)
			cout << ans.a[i][j] << ' ';
		cout << endl;
	}	*/
	cout << ans.a[1][1] + sum << endl;

	return 0;
}//hello


//teja349
#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
//setbase - cout << setbase (16); cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >

//std::ios::sync_with_stdio(false);
ll x,k,n,q,iinf,counter=0,c[100];
map<ll,ll> inv,mapi,special;
ll dp[75][75],res[75],res1[75],reserv[75][75],haha[75][75];
ll hshit(ll i){
	ll jj=i,cnt=0;
	while(i>0){
		if(i%2)
			cnt++;
		i/=2;
	}
	i=jj;
	if(cnt!=x){
		return 0;
	}
	mapi[i]=counter;
	inv[counter]=i;
	counter++;
	return 0;
}
ll updatedp(){
	ll i,j,k;
	rep(i,counter){
		rep(j,counter){
			haha[i][j]=iinf;
		}
	}
	rep(i,counter){
		rep(j,counter){
			rep(k,counter){
				haha[i][k]=min(haha[i][k],dp[i][j]+dp[j][k]);
			}
		}
	}
	rep(i,counter){
		rep(j,counter){
			dp[i][j]=haha[i][j];
		}
	}
}
ll updateres(){
	ll i,j;
	rep(i,counter){
		res1[i]=iinf;
	}
	rep(i,counter){
		rep(j,counter){
			res1[j]=min(res1[j],res[i]+dp[i][j]);
		}
	}
	rep(i,counter){
		res[i]=res1[i];
	}
	return 0;
}
ll updatefrom(ll s,ll e){

	e=min(e,n);
	if(e<=s){
		return 0;
	}
	//cout<<s<<"fdf "<<e<<endl;
	//return 0;
	//cout<<"boo"<<endl;
	ll i,j;
	ll len=e-s;
	rep(i,counter){
		rep(j,counter){
			dp[i][j]=reserv[i][j];
		}
	}
	while(len>0){
		if(len%2){
			//cout<<"Dasd"<<endl;

			updateres();
		}

			//cout<<"Dsasd"<<endl;
			// rep(i,counter){
			// 	rep(j,counter){
			// 		cout<<inv[i]<<" "<<inv[j]<<" "<<dp[i][j]<<endl;
			// 	}
			// }
			updatedp();

		len/=2;
	}
	return 0;
}
ll initiall(ll val){
	//cout<<val<<endl;
	ll wow1,wow=inv[val];
	if(wow%2==0){
		reserv[val][mapi[wow/2]]=0;
		return 0;
	}
	wow1=wow;
	wow/=2;
	ll i,j;
	rep(i,k){
		if(wow&(1LL<<i)){
			continue;
		}
		wow1=wow+(1<<i);
		reserv[val][mapi[wow1]]=c[i+1];
		//cout<<i<<"dsd "<<c[i+1]<<endl;
	}
	return 0;
}
ll manual(ll s,ll e){
	e=min(e,n);
	if(e<=s)
		return 0;
	//cout<<s<<" "<<e<<endl;
	//return 0;
	ll i,j,p,wow,wow1,wow2;
	f(i,s,e){
		rep(j,counter){
			res1[j]=iinf;
			//cout<<res[j]<<"dsd "<<endl;
		}
		//return 0;
		rep(j,counter){
			//cout<<"Dasd"<<endl;
			wow=inv[j];
			if(wow%2==0){
				res1[mapi[wow/2]]=min(res1[mapi[wow/2]],res[j]);
				continue;
			}
			wow2=wow;
			wow2=mapi[wow2];
			wow/=2;
			//cout<<res[1]<<endl;
			rep(p,k){
				//cout<<p<<endl;
				if((wow&(1LL<<p))==0){

					if(special.find(i+p+1)!=special.end()){
						//cout<<"dasdsa"<<endl;
						wow1=wow+(1<<p);
						wow1=mapi[wow1];

						res1[wow1]=min(res1[wow1],res[wow2]+c[p+1]+special[i+p+1]);
					}
					else{
						wow1=wow+(1<<p);
						//cout<<res[wow2]+c[p+1]<<endl;
						wow1=mapi[wow1];
						//wow2=mapi[wow2];
						res1[wow1]=min(res1[wow1],res[wow2]+c[p+1]);

					}
				}
			}
			//return 0;
		}
		//return 0;
		rep(j,counter){
			res[j]=res1[j];
		}
	}
	return 0;
}
ll sp[123],wt[123];
vll vec;
int main(){
    std::ios::sync_with_stdio(false);
    iinf =inf;
    iinf*=inf;
    cin>>x>>k>>n>>q;
    n-=x;
   	n++;
   	ll i,j;
    f(i,1,k+1){
    	cin>>c[i];
    	//cout<<c[i]<<endl;
    }
    ll init=(1<<x)-1;
    rep(i,(1<<k)){
    	hshit(i);
    }
    //return 0;
    rep(i,counter){
    	rep(j,counter){
    		reserv[i][j]=iinf;
    		res[j]=iinf;
    	}
    }

    rep(i,counter){
    	initiall(i);
    }
    // rep(i,counter){
    // 	rep(j,counter){
    // 		cout<<inv[i]<<" "<<inv[j]<<" "<<reserv[i][j]<<endl;
    // 	}
    // }
    res[mapi[init]]=0;
   	q++;
   	sp[0]=inf;
   	wt[0]=inf;
   	special[inf]=inf;
   	vec.pb(mp(sp[0],wt[0]));
   	f(i,1,q){
   		cin>>sp[i]>>wt[i];
   		special[sp[i]]=wt[i];
   		vec.pb(mp(sp[i],wt[i]));
   	}
   	sort(all(vec));
   	rep(i,q){
   		sp[i]=vec[i].ff;
   		wt[i]=vec[i].ss;
   	}
   	ll previ=1;
   	//return 0;
   	//updatefrom(1,n);
   	//manual(1,4);
   	//cout<<res[mapi[init]]<<endl;
   	//return 0;
   	rep(i,q){
   		//cout<<"Dsads"<<endl;
   		updatefrom(previ,sp[i]-k);
   		//cout<<previ<<" "<<sp[i]-k<<endl;
   		previ=max(previ,sp[i]-k);
   		//cout<<previ<<" "<<sp[i]<<endl;
   		manual(max(previ,sp[i]-k),sp[i]);
   		previ=max(previ,sp[i]);
   	}

   	cout<<res[mapi[init]]<<endl;

    return 0;

}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <vector>

#include <functional>
#include <complex>
#include <valarray>
#include <cassert>

using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) 42
#endif

typedef long long ll;
#define X first
#define Y second
#define mp make_pair

const int N = 20;
int n, k;
double p[N];
double dp[(1 << N) + 7];
double ans[N];

int main()
{
#ifdef LOCAL
	freopen ("input.txt", "r", stdin);
	freopen ("output.txt", "w", stdout);
#endif

	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; i++)
		scanf("%lf", &p[i]);
	int cnt = 0;
	for (int i = 0; i < n; i++)
		if (p[i] > 0.000001)
			cnt++;
	if (cnt <= k)
	{
		for (int i = 0; i < n; i++)
			if (p[i] > 0.000001)
				printf("1 ");
			else
				printf("0 ");
		printf("\n");
		return 0;
	}
	dp[0] = 1;
	for (int mask = 0; mask < (1 << n); mask++)
	{
		double sum = 0;
		for (int i = 0; i < n; i++)
			if ((mask >> i) & 1)
				sum += p[i];
		if (sum >= 0.99999) continue;
		for (int i = 0; i < n; i++)
		{
			if ((mask >> i) & 1) continue;
			dp[mask | (1 << i)] += dp[mask] * p[i] / (1 - sum);
		}
	}
	for (int mask = 0; mask < (1 << n); mask++)
	{
		cnt = 0;
		for (int i = 0; i < n; i++)
			if ((mask >> i) & 1)
				cnt++;
		if (cnt != k) continue;
		for (int i = 0; i < n; i++)
			if ((mask >> i) & 1)
				ans[i] += dp[mask];
	}
	for (int i = 0; i < n; i++)
		printf("%.13lf ", ans[i]);
	printf("\n");

	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cassert>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define forn(i, n) for (int i = 0; i < (int) n; ++i)
#define ford(i, n) for (int i = ((int) n) - 1; i >= 0; --i)
#define sz(c) ((int) (c).size())
#define mp(x, y) make_pair(x, y)
#define prev prev___________________
#define hash hash___________________
#define pb push_back

#define FILE_NAME ""

void solve (int n)
{
    int k;
    cin >> k;

    typedef double T;
    vector<T> p(n);
    for (int i = 0; i < n; i++)
        cin >> p[i];

    T dp[2][1 << n];
    memset(dp, 0, sizeof dp);

    vector<char> isgood(n);
    vi good;
    for (int i = 0; i < n; i++)
    if (p[i] != 0)
    {
        isgood[i] = 1;
        good.pb(i);
    }

    dp[0][0] = 1;
    int c = 1;

    vector<T> sum(1 << n);
    sum[0] = 1.0;
    for (int j = 1; j < (1 << n); j++)
    {
        int x = __builtin_ctz(j);
        assert((j >> x) & 1);
        sum[j] = sum[j ^ (1 << x)] - p[x];
    }

    for (int i = 0; i < min(sz(good), k); i++)
    {
        memset(dp[c], 0, sizeof dp[c]);

        for (int j = 0; j < (1 << n); j++)
        {
            if (__builtin_popcount(j) != i)
                continue;

            for (int next : good)
            if (!((j >> next) & 1))
            {
                dp[c][j ^ (1 << next)] += dp[c ^ 1][j] * p[next] / sum[j];
            }
        }

        c ^= 1;
    }

    c ^= 1;
    for (int i = 0; i < n; i++)
    {
        if (!isgood[i])
            printf("%.12f ", (double)0);
        else
        {
            T ans = 0;
            for (int j = 0; j < (1 << n); j++)
            if (((j >> i) & 1))
                ans += dp[c][j];
            printf("%.12f ", (double)ans);
        }
        puts("");
    }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen(FILE_NAME ".in", "r", stdin);
	freopen(FILE_NAME ".out", "w", stdout);
#endif

	int n;
	while (cin >> n)
		solve(n);

#ifdef LOCAL
	cerr << "Time: " << (double) clock() / CLOCKS_PER_SEC << endl;
#endif
	return 0;
}


#pragma comment(linker, "/STACK:256000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#undef NDEBUG
#include <cassert>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <tuple>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define pbk pop_back
#define mt make_tuple
#define sz(s) ((int) (s).size())
#define all(x) (x).begin(), (x).end()
#define next hunext
#define rank hurank
#define prev huprev
#define link hulink
#define hash huhash
#define y0 yy0
#define y1 yy1
#ifdef LOCAL42
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#define cerr if (false) cerr
#endif

typedef long long ll;
typedef long long llong;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef unsigned long long ullong;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long double ld;
typedef long double ldouble;

const int inf = 1e9;
const double pi = 4 * atan(1.0);
const double eps = 1e-9;

const int N = 20;

double _P[N];
double P[N];

double D[1 << N];
double sum[1 << N];

double Q[N];

int ind[N];

int main() {
#define TASK "A"
#ifdef LOCAL42
	freopen(TASK ".in", "r", stdin);
	freopen(TASK ".out", "w", stdout);
#else

#endif

	int _n, k;
	scanf("%d %d", &_n, &k);

	int n = 0;

	for (int i = 0; i < _n; i++) {
		scanf("%lf", &_P[i]);
		if (_P[i] > eps)
			P[n] = _P[i], ind[i] = n, n++;
		else
			ind[i] = -1;

	}

	k = min(k, n);

	D[0] = 1;
	sum[0] = 0;

	for (int i = 1; i < (1 << n); i++) {
		for (int j = 0; j < n; j++) {
			if ((i >> j) & 1) {
				sum[i] = sum[i ^ (1 << j)] + P[j];
				break;
			}
		}
	}

	for (int msk = 0; msk < (1 << n); msk++) {
		for (int i = 0; i < n; i++) {
			if ((msk >> i) & 1) {
				continue;
			}
			assert(P[i] > eps);
			D[msk ^ (1 << i)] += D[msk] * (P[i]) / (1 - sum[msk]);
		}
	}

	for (int msk = 0; msk < (1 << n); msk++) {
		int cnt = 0;
		for (int i = 0; i < n; i++) {
			if ((msk >> i) & 1) {
				cnt++;
			}
		}
		if (cnt == k) {
			for (int i = 0; i < n; i++) {
				if ((msk >> i) & 1)
					Q[i] += D[msk];
			}
		}
	}

	for (int i = 0; i < _n; i++) {
		if (ind[i] == -1)
			printf("0 ");
		else
			printf("%.10lf ", Q[ind[i]]);
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define sz(a) ((int) (a).size())
#define int64 long long
#define ldb long double
const ldb pi = acos(-1.0);

const int N = 20;
const double eps = 1e-10;
int n, k;
double a[N], p[1 << N], res[N];

int main() {
	// assert(freopen("input.txt", "r", stdin));
	// assert(freopen("output.txt", "w", stdout));
	scanf("%d%d", &n, &k);
	int cnt = 0;
	for (int i = 0; i < n; ++i) {
		scanf("%lf", &a[i]);
		if (a[i] < eps) ++cnt;
	}
	k = min(n - cnt, k);
	p[0] = 1;
	for (int mask = 1; mask < (1 << n); ++mask) {
		double suma = 0;
		bool ok = true;
		int cnt = 0;
		for (int i = 0; i < n; ++i)
			if (mask & (1 << i)) {
				suma += a[i];
				if (a[i] < eps) ok = false;
				++cnt;
			}
		if (!ok) {
			p[mask] = 0;
			continue;
		} else {
			double sump = 0;
			for (int i = 0; i < n; ++i)
				if (mask & (1 << i))
					sump += a[i] / (1 - suma + a[i]) * p[mask ^ (1 << i)];
			p[mask] = sump;
		}
		if (cnt == k)
			for (int i = 0; i < n; ++i)
				if (mask & (1 << i))
					res[i] += p[mask];
	}
	for (int i = 0; i < n; ++i)
		printf("%.10f%c", res[i], " \n"[i == n - 1]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)
#define FILL(A,val) memset(A,val,sizeof(A))
#define MP make_pair
#define PB push_back
#define ALL(A) A.begin(),A.end()
#define SZ(A) (int)A.size()

typedef long long Int;
typedef unsigned long long UInt;
typedef pair<int, int> PII;
typedef vector<int> VI;

const int INF = 1000000000;
const int MAX = 1000000;
const int ST = 1000000007;

double dp[1 << 20];

int cnt[1 << 20];

double res[22];

double p[22];

int main()
{
    //freopen("in.txt" , "r" , stdin);

    FOR(i,1,1 << 20)
    {
        cnt[i] = cnt[i / 2] + i % 2;
    }

    int n , k;
    cin >> n >> k;
    FOR(i,0,n)
    {
        cin >> p[i];
    }

    dp[0] = 1;

    FOR(mask , 0 , 1 << 20)
    {
        double sum = 0;
        FOR(i,0,n)
        {
            if (!(mask & (1 << i)))
            {
                sum += p[i];
            }
        }

        if (sum < 1e-6) continue;

        FOR(i,0,n)
        {

            if (!(mask & (1 << i)))
            {
                if (cnt[mask] < k)
                {
                    res[i] += dp[mask] * p[i] / sum;
                }
                dp[mask | (1 << i)] += dp[mask] * p[i] / sum;

            }
        }
    }

    FOR(i,0,n)
    {
        printf("%.12f " , res[i]);

    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sz(a) ((int)(a).size())
#define null NULL
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define relaxMin(a, b) (a) = min((a), (b))
#define relaxMax(a, b) (a) = max((a), (b))
#define PI 3.1415926535897932384
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

#define BITS(a) __builtin_popcount(a)
const double EPS = 1E-9;

typedef long double dbl;

typedef pair<dbl, dbl> Val; // a * fi + se

Val Add(const Val& f, const Val& s){
  return mp(f.fi + s.fi,
            f.se + s.se);
}

Val Sub(const Val& f, const Val& s){
  return mp(f.fi - s.fi,
            f.se - s.se);
}

Val Mult(const Val& f, dbl w){
  return mp(f.fi * w,
            f.se * w);
}

Val Div(const Val& f, dbl w){
  return mp(f.fi / w,
            f.se / w);
}

Val dp[1 << 20];
int n, k;
double p[25];

double Solve(int vr){
  if(p[vr] < EPS) return 0.0;

  dp[0] = Val(1.0, 0.0);
  for(int msk = 1;msk < (1 << n);++msk){
    if(msk & (1 << vr)) continue;
    //Val ri = Mult(Val(1.0, 0.0), p[vr]);
    Val ri(0.0, 0.0);
    dbl same = 0.0;

    for(int i = 0;i < n;++i)
      if(i != vr)
        if(msk & (1 << i)){
          same += p[i];
          ri = Add(ri, Mult(dp[msk ^ (1 << i)], p[i]));
        }

    //cout << ri.fi << ' ' << ri.s << endl;
    dp[msk] = Div(ri, 1.0 - same);
  }

  //cout << dp[2].fi << ' ' << dp[2].se << endl;

  Val tot(1.0, 0.0);
  for(int msk = 1;msk < (1 << n);++msk)
    if(!(msk & (1 << vr))){
      tot = Add(tot, dp[msk]);
    }

  dbl alp = (1.0 - tot.se) / tot.fi;

  Val bad(0.0, 0.0);
  for(int msk = 0;msk < (1 << n);++msk){
    if(msk & (1 << vr)) continue;
    if(BITS(msk) >= k) bad = Add(bad, dp[msk]);
  }

  return 1.0 - (alp * bad.fi + bad.se);
}

int main(){
  scanf("%d%d", &n, &k);
  for(int i = 0;i < n;++i)
    scanf("%lf", &p[i]);

  for(int i = 0;i < n;++i)
    printf("%.12f%c", Solve(i), (i + 1 == n ? '\n' : ' '));

  return 0;
}


#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <cmath>
#include <ctime>

using namespace std;

#define all(v) (v).begin(), (v).end()

using li = long long;
using vi = vector<int>;
void solve();

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	auto start = clock();
#endif
	cout.sync_with_stdio(0);
	cin.tie(0);
	cout.precision(10);
	cout << fixed;
	int t;
	//cin >> t;
	t = 1;
	while (t--) {
		solve();
	}
#ifdef LOCAL
	cerr << endl << endl << (clock() - start) / double(CLOCKS_PER_SEC) << endl;
#endif
	return 0;
}

//#define int li

auto& in = std::cin;
auto& out = std::cout;

void solve() {
	int n, k;
	in >> n >> k;
	vector<double> allp(n);

	for (int i = 0; i < n; ++i) {
		in >> allp[i];
	}

	for (int i = 0; i < n; ++i) {
		if (fabs(allp[i]) < 1e-6) {
			out << "0 ";
			continue;
		}
		vector<double> dp(1 << (n - 1));
		vector<double> p(n - 1);
		int c = 0;
		for (int j = 0; j < n; ++j) {
			if (i != j) {
				p[c++] = allp[j];
			}
		}
		dp[0] = allp[i];
		double ans = dp[0];
		for (int mask = 1; mask < (1 << (n - 1)); ++mask) {
			double sumP = 0;
			double sum = 0;
			int cnt = 0;
			for (int t = 0; t < (n - 1); ++t) {
				if (mask & (1 << t)) {
					sumP += p[t];
					sum += p[t] * dp[mask ^ (1 << t)];
					++cnt;
				}
			}
			dp[mask] = sum / (1 - sumP);
			if (cnt < k) {
				ans += dp[mask];
			}
		}
		out << ans << " ";

	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <ctime>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
	freopen("task.in", "r", stdin);
	freopen("task.out", "w", stdout);
#endif

	cout.precision(15);
	cout << fixed;

	int n, k;
	while (cin >> n >> k) {
		vector<double> p(n);
		for (int i = 0; i < n; i++) cin >> p[i];

		vector<double> sum(1 << n);
		for (int i = 0; i < 1 << n; i++) {
			for (int j = 0; j < n; j++) {
				if (i & (1 << j)) {
					sum[i] += p[j];
				}
			}
		}

		int cnt = 0;
		for (int i = 0; i < n; i++) {
			cnt += p[i] > 1e-5;
		}
		if (cnt <= k) {
			for (int i = 0; i < n; i++) {
				double pp;
				if (p[i] > 1e-5) pp = 1.0;
				else pp = 0.0;
				cout << pp << " \n"[i + 1 == n];
			}
			continue;
		}
		vector<double> dp(1 << n);
		dp[0] = 1;
		for (int mask = 0; mask < 1 << n; mask++) {
			int rmask = (~mask) & ((1 << n) - 1);
			for (int i = 0; i < n; i++) if ((rmask & (1 << i)) && p[i] > 1e-5) {
				dp[mask | (1 << i)] += dp[mask] * p[i] / sum[rmask];
			}
		}
		vector<double> ans(n);
		for (int mask = 0; mask < 1 << n; mask++) {
			int cnt = 0;
			for (int i = 0; i < n; i++) cnt += (mask & (1 << i)) != 0;
			if (cnt != k) continue;
			for (int i = 0; i < n; i++) if (mask & (1 << i)) {
				ans[i] += dp[mask];
			}
		}
		for (int i = 0; i < n; i++) cout << ans[i] << " \n"[i + 1 == n];
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin (),(x).end()
#define sqrt(x) sqrt(abs(x))
#define re return
#define sz(x) ((int)(x.size()))

using ll = long long;
using ii = pair<int, int>;
using ld = long double;
using D = double;
using vi = vector<int>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vs = vector<string>;

template<typename T> T abs (T x) { re x < 0 ? -x : x; }
template<typename T> T sgn (T x) { re x < 0 ? -1 : (x > 0 ? 1 : 0); }
template<typename T> T sqr (T x) { re x * x; }
template<typename T> T gcd (T a, T b) { re a ? gcd (b % a, a) : b; }

int n;
int m;

double p[20];
double res[1 << 20];
double q[20];

int main () {
	scanf ("%d%d", &n, &m);
	for (int i = 0; i < n; i++) scanf ("%lf", &p[i]);
	for (int i = 0; i < n; i++) res[1 << i] = p[i];
	for (int i = 0; i < n; i++) q[i] = 0;
	for (int i = 1; i < (1 << n); i++) {
		vi v, w;
		double sum = 0;
		for (int j = 0; j < n; j++)
			if ((i >> j) & 1)
				v.pb (j);
			else {
				w.pb (j);
				sum += p[j];
			}
		if (sz (v) == m || (sz (v) < m && sum < 1e-5)) {
			for (auto x : v) q[x] += res[i];
		} else
		if (sz (v) < m) {
			for (auto x : w) res[i | (1 << x)] += res[i] * p[x] / sum;
		}
	}
	for (int i = 0; i < n; i++) printf ("%.15f ", q[i]);
	printf ("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
typedef long long i64;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long double ld;

const int M = 21;

const ld eps = 1e-6;

int n = 0, k, m;
ld p[M];
int id[M];
ld d[1 << M];
ld ans[M];

void read() {
    cin >> m >> k;
    for (int i = 0; i < m; ++i) {
        ld x;
        cin >> x;
        if (x > eps) {
            p[n] = x;
            id[n] = i;
            ++n;
        } else {
            ans[i] = 0;
        }
    }
}

void kill() {
    if (k >= n) {
        for (int i = 0; i < n; ++i)
            ans[id[i]] = 1.0;
    } else {
        d[0] = 1.0;

        for (int mask = 0; mask < (1 << n); ++mask)
            if (__builtin_popcount(mask) < k) {
                ld sum = 0.0;

                for (int t = 0; t < n; ++t)
                    if (((mask >> t) & 1) == 0) {
                        sum += p[t];
                    }

                for (int t = 0; t < n; ++t)
                    if (!((mask >> t) & 1)) {
                        d[mask ^ (1 << t)] += p[t] / sum * d[mask];
                    }
            }

        for (int mask = 0; mask < (1 << n); ++mask)
            if (__builtin_popcount(mask) == k) {
                for (int t = 0; t < n; ++t)
                    if ((mask >> t) & 1) {
                        ans[id[t]] += d[mask];
                    }
            }
    }

    for (int i = 0; i < m; ++i)
        cout << ans[i] << " ";
    cout << endl;
}

signed main() {
#ifdef LOCAL
    assert(freopen("a.in", "r", stdin));
#endif

    cout.precision(10);
    cout << fixed;

    read();
    kill();

#ifdef LOCAL
    cerr << "Time: " << clock()/1000 << " ms" << endl;
#endif
}


//#pragma comment(linker, "/STACK:16777216")
#include <fstream>
#include <iostream>
#include <bitset>
#include <cstring>
#include <string>
#include <complex>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <unordered_set>
#include <unordered_map>

using namespace std;

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

using ll = long long;
using pii = pair<int, int>;
template<class T1, class T2, class T3> struct triple {
	T1 x; T2 y; T3 z;
};
using tri = triple<int, int, int>;
using trll = triple<ll, ll, ll>;
template<class T1, class T2, class T3>
bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
	return t1.x < t2.x || (t1.x == t2.x && (t1.y < t2.y || t1.y == t2.y && t1.z < t2.z));
}

#define eps 1e-9
#define bs 1000000007
const int mod = 1000000007;
const int INF = 1000111000;
const ll LLINF = 1000111000111000111LL;
const double PI = acos(-1.0);
#define bsize 256

#define all(v) (v).begin(), (v).end()
#define FI(n) for (int i = 0; i < n; ++i)
#define FJ(n) for (int j = 0; j < n; ++j)
#define FK(n) for (int k = 0; k < n; ++k)
#define FREIN(file) freopen(file, "rt", stdin)
#define FREOUT(file) freopen(file, "wt", stdout)
#define sqr(x) ((x)*(x))

#define DB(n) cerr<<#n<<" = "<<(n)<<" "
#define DBN(n) cerr<<#n<<" = "<<(n)<<"\n"

int gcd(int x, int y) { while (y) { x %= y, swap(x, y); } return y; }
ll gcd(ll x, ll y) { while (y) { x %= y, swap(x, y); } return y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll powmod(ll x, ll n, const int mod) {
	ll res = 1; while (n)
	{
		if (n & 1) res = res * x % mod; n /= 2; x = x * x % mod;
	} return res;
}

int n, k;
double prob[200];
double dp[1 << 20];
double sum[1 << 20];
double ans[22];

int count_bits(int x)
{
	if (x == 0)
		return 0;
	return count_bits(x / 2) + x % 2;
}

int flag;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> prob[i];
	}

	for (int mask = 0; mask < (1 << n); mask++)
	{
		double s = 0;
		for (int i = 0; i < n; i++)
		{
			if (mask&(1 << i))
				s += prob[i];
		}
		sum[mask] = s;
	}

	dp[0] = 1;

	for (int mask = 0; mask < (1 << n); mask++)
	{
		int qb = count_bits(mask);
		if (qb > k)
			continue;
		if (dp[mask]<1e-100)
			continue;
		if (qb == k)
			flag = 1;
		double rem_sum = 1.0 - sum[mask];
		if (rem_sum < 1e-6)
			continue;
		for (int i = 0; i < n; i++)
		{
			if (mask&(1 << i))
				continue;
			double chance_move = dp[mask] * prob[i] / rem_sum;
			dp[mask | (1 << i)] += chance_move;
		}
	}

	if (flag == 0)
	{
		for (int i = 0; i < n; i++)
		{
			if (prob[i] < 1e-6)
				ans[i] = 0;
			else
				ans[i] = 1;
		}
	}
	else
	{
		for (int mask = 0; mask < (1 << n); mask++)
		{
			if (count_bits(mask) == k)
			{
				for (int i = 0; i < n; i++)
				{
					if (mask&(1 << i))
						ans[i] += dp[mask];
				}
			}
		}
	}

	cout.precision(12);
	for (int i = 0; i < n; i++)
	{
		if (i)
			cout << " ";
		cout << fixed << ans[i];
	}
	cout << endl;

	return 0;
}


//# pragma GCC optimize ("O2")
//# pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
#define lb(x) ((x)&(-x))
#define out(i,a,now) for(LL i=a.be[now],to=a.v[i];~i;i=a.ne[i],to=a.v[i])
#define fo(i,a,b) for(i=(a);i<=(b);++i)
#define fd(i,a,b) for(i=(a);i>=(b);--i)
#define mid ((l+r)>>1)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
#define mp(a,b) make_pair(a,b)
#define qk(x) memset(x,0,sizeof(x))
#define RG register
#define SF scanf
#define PF printf
#define FP freopen
#define FI first
#define SE second
#define cpy(a,b) memcpy(a,b,sizeof(a))//b to a
#define pi 3.1415926535897932384626433832
#define ls (now<<1)
#define rs (now<<1|1)
#ifdef Mogician
#endif
using namespace std;
typedef long long LL;
typedef double DB;
typedef pair<LL,LL> Pair;
template <class T> void cmax(T &a,T b){
    a=max(a,b);
}
template <class T> void cmin(T &a,T b){
    a=min(a,b);
}
const LL maxn=140000,kcz=998244353;
LL ksm(LL ds,LL zs=kcz-2,LL mod=kcz){
	LL res=1;
	for(;zs;zs>>=1,ds=ds*ds%mod)
		if (zs&1) res=res*ds%mod;
	return res;
}
const LL i100=ksm(100);
LL lim[maxn][2][2],n;
LL p[maxn][2];
struct SEG{
	LL tr[maxn<<1][2][2];
	void up(LL now,LL l,LL r){
		qk(tr[now]);
		LL i,j,k,t;
		fo(i,0,1) fo(j,0,1) fo(k,0,1) fo(t,0,1)
			if (!lim[mid][k][t]) (tr[now][i][j]+=tr[ls][i][k]*tr[rs][t][j])%=kcz;
	}
	void build(LL now,LL l,LL r){
		if (l==r){
			tr[now][0][0]=p[l][0];
			tr[now][1][1]=p[l][1];
			return;
		}
		build(ls,l,mid); build(rs,mid+1,r);
		up(now,l,r);
	}
	void up(LL now,LL l,LL r,LL x){
		if (l==r) return;
		if (mid>=x) up(ls,l,mid,x);
		else up(rs,mid+1,r,x);
		up(now,l,r);
	}
}seg;
struct data{
	LL id,l,r,t;
	DB p;
	data(){
		id=l=r=t=0; p=0;
	}
	data(LL a,LL b,LL c,LL d,DB e){
		id=a; l=b; r=c; t=d; p=e;
	}
	bool operator <(const data &b) const{
		return p<b.p;
	}
}a[maxn<<1];
int main(){
	SF("%lld",&n);
	LL i,lx,lv,cnt=0;
	fo(i,1,n){
		LL X,V,P;
		SF("%lld%lld%lld",&X,&V,&P);
		if (i>1){
			a[++cnt]=data(i-1,1,0,(X-lx)*ksm(V+lv)%kcz,(DB)(X-lx)/(V+lv));
			if (V>lv)
				a[++cnt]=data(i-1,0,0,(X-lx)*ksm(V-lv)%kcz,(DB)(X-lx)/(V-lv));
			else if (lv>V)
				a[++cnt]=data(i-1,1,1,(X-lx)*ksm(lv-V)%kcz,(DB)(X-lx)/(lv-V));
		}
		p[i][0]=(100-P)*i100%kcz;
		p[i][1]=P*i100%kcz;
		lv=V; lx=X;
	}
	seg.build(1,1,n);
	sort(a+1,a+cnt+1);
	LL ans=0,last=1,now,j,k;
	fo(i,1,cnt){
		lim[a[i].id][a[i].l][a[i].r]=1;
		seg.up(1,1,n,a[i].id);
		now=0;
		fo(j,0,1) fo(k,0,1) (now+=seg.tr[1][j][k])%=kcz;
		ans+=a[i].t*(last-now+kcz)%kcz;
		last=now;
	}
	PF("%lld",(ans%kcz+kcz)%kcz);
	return 0;
}