Submission

Status:

[PP][-S][-S][-S][-S][-S][-S][-S][-S][-S]

Subtask/Task Score:

{10/10}{0/10}{0/10}{0/10}{0/10}{0/10}{0/10}{0/10}{0/10}{0/10}

Score: 10

User: qweqwe

Problemset: รถขนส่ง

Language: cpp

Time: 0.004 second

Submitted On: 2026-02-25 23:40:48

#include <bits/stdc++.h>
#define fastio cin.tie(0)->sync_with_stdio(0)
using namespace std;
using ll = long long;
using pii = pair<ll,ll>;
using db = long double;

int main(){
	fastio;
	int n,k;
	cin >> n >> k;
	
	vector<vector<pii>> graph(303);
	vector<pii> costsToF,costsFromF;
	vector<bool> visited(n*2,0);
	
	for (int i=1;i<=n;i++){
		int cost;
		cin >> cost;
		costsToF.push_back({cost,1});
	}
	
	for (int i=1;i<=n;i++){
		int cost;
		cin >> cost;
		costsToF.push_back({cost,2});
	}
	
	for (int i=n+1;i<=2*n;i++){
		int cost;
		cin >> cost;
		costsFromF.push_back({cost,1});
	}
	
	for (int i=n+1;i<=2*n;i++){
		int cost;
		cin >> cost;
		costsFromF.push_back({cost,2});
	}
	
	sort(costsToF.begin(),costsToF.end());
	sort(costsFromF.begin(),costsFromF.end(),greater<pii>());
	
	ll cnt = 0;
	vector<ll> dp(2*n,LLONG_MAX);
	
	while (cnt < 2*n){
		vector<ll> indices;
		ll index = 0,prevIndex = 0;
		bool found = 0;
		
		while (index < (ll)costsFromF.size()){
			if (costsFromF[index].second==costsToF[cnt].second){
				if (!found) prevIndex = index;
				indices.push_back(index);
				found = 1;
			}index++;
		}
		
		for (ll i:indices){
			//cout << prevIndex << " " << i << " " << cnt << " " << costsFromF[i].first+costsToF[cnt].first << "\n";
			if (costsFromF[i].second==costsToF[cnt].second){
				dp[cnt] = min(dp[cnt],costsFromF[i].first+costsToF[cnt].first);
			}
		}
		cnt++;
	}
	
	sort(dp.begin(),dp.end());
	
	cout << dp[k-1];
	return 0;
}
/*
3 6
3 5 10
4 6 12
2 1 8
3 7 5
*/