using
System;
using
System.Collections.Generic;
class
GFG {
static
List<
int
> Helper(List<
int
> nums,
int
c_len)
{
List<
int
> ans =
new
List<
int
>();
int
ln = nums.Count;
for
(
int
i = 0; i < ln; i++) {
while
(ans.Count > 0
&& ans[ans.Count - 1] < nums[i]
&& ((ln - i) > (c_len - ans.Count))) {
ans.RemoveAt(ans.Count - 1);
}
if
(ans.Count < c_len) {
ans.Add(nums[i]);
}
}
return
ans;
}
static
bool
Comp(List<
int
> a1, List<
int
> a2)
{
int
s1 = a1.Count;
int
s2 = a2.Count;
int
i1 = 0, i2 = 0;
while
(i1 < s1 && i2 < s2) {
if
(a1[i1] > a2[i2]) {
return
true
;
}
else
if
(a1[i1] < a2[i2]) {
return
false
;
}
i1++;
i2++;
}
if
(i1 < s1)
return
true
;
return
false
;
}
static
List<
int
> MaxNumber(List<
int
> nums1,
List<
int
> nums2,
int
k)
{
int
l1 = nums1.Count;
int
l2 = nums2.Count;
List<
int
> rs =
new
List<
int
>();
for
(
int
s1 = Math.Max(0, k - l2);
s1 <= Math.Min(k, l1); s1++) {
List<
int
> p1 =
new
List<
int
>();
List<
int
> p2 =
new
List<
int
>();
p1 = Helper(nums1, s1);
p2 = Helper(nums2, k - s1);
List<
int
> temp =
new
List<
int
>();
for
(
int
j = 0; j < k; j++) {
List<
int
> temp2 = Comp(p1, p2) ? p1 : p2;
int
fr = temp2[0];
if
(Comp(p1, p2)) {
if
(p1.Count > 0) {
p1.RemoveAt(0);
}
}
else
{
if
(p2.Count > 0) {
p2.RemoveAt(0);
}
}
temp.Add(fr);
}
rs = Comp(rs, temp) ? rs : temp;
}
return
rs;
}
public
static
void
Main(
string
[] args)
{
List<
int
> arr1 =
new
List<
int
>{ 3, 4, 6, 5 };
List<
int
> arr2 =
new
List<
int
>{ 9, 1, 2, 5, 8, 3 };
int
k = 5;
List<
int
> result = MaxNumber(arr1, arr2, k);
foreach
(
int
i
in
result) { Console.Write(i +
" "
); }
}
}