Arkady decided to buy roses for his girlfriend.A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet.

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O'  — orange, 'R' — red.

Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1.

NoteIn the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color.

import java.io.*;
import java.util.*;

public class Main {

    private static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException exp) {
                    throw new RuntimeException(exp);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }
    }

    public static void main(String[] args) {    //throws FileNotFoundException {
        InputStream inputStream = System.in;    //new FileInputStream("input.txt");
        OutputStream outputStream = System.out; //new FileOutputStream("output.txt");
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        Task newTask = new Task();
        newTask.solve(in, out);

        out.close();
    }

    private static class Task {
        //.constant
        private final int INF = Integer.MAX_VALUE;
        private final int MOD = (int)1e9 + 7;

        //.data
        class Rose implements Comparable<Rose> {
            public int b = 0;
            public char c = 0;
            public int compareTo(Rose other) {
                return -Integer.compare(b, other.b);
            }
        }

        //.code
        public void solve(InputReader in, PrintWriter out) {
            int n = in.nextInt();
            Rose[] rose = new Rose[n];
            int k = in.nextInt();
            for (int i = 0; i < n; ++i) {
                rose[i] = new Rose();
                rose[i].b = in.nextInt();
            }
            String s = in.next();
            for (int i = 0; i < n; ++i) {
                rose[i].c = s.charAt(i);
            }
            Arrays.sort(rose);

            int ans = -1;
            int cntO = 0;
            int cntD = 0;
            int sum = 0;
            for (Rose r : rose) {
                if (r.c != 'W') {
                    if (r.c == 'O' && cntO != k - 1) {
                        ++cntO;
                        sum += r.b;
                    } else if (r.c == 'R' && cntD != k - 1) {
                        ++cntD;
                        sum += r.b;
                    }
                }
                if (cntO + cntD == k) {
                    ans = sum;
                    break;
                }
            }
            cntO = 0;
            cntD = 0;
            sum = 0;
            for (Rose r : rose) {
                if (r.c != 'R') {
                    if (r.c == 'O' && cntO != k - 1) {
                        ++cntO;
                        sum += r.b;
                    } else if (r.c == 'W' && cntD != k - 1) {
                        ++cntD;
                        sum += r.b;
                    }
                }
                if (cntO + cntD == k) {
                    ans = Math.max(ans, sum);
                    break;
                }
            }

            out.println(ans);

        }//end

    }

}

import java.io.*;
import java.util.*;

public class temp
{
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int k=in.nextInt();
        int[] b=new int[n];
        for(int i=0;i<n;i++)
            b[i]=in.nextInt();
        char[] t=in.next().toCharArray();
        ArrayList<Integer> w=new ArrayList<Integer>();
        ArrayList<Integer> o=new ArrayList<Integer>();
        ArrayList<Integer> r=new ArrayList<Integer>();
        for(int i=0;i<n;i++)
        {
            if(t[i]=='R')
                r.add(b[i]);
            else if(t[i]=='O')
                o.add(b[i]);
            else
                w.add(b[i]);
        }
        Collections.sort(w);
        Collections.sort(o);
        Collections.sort(r);
        int s[]=new int[w.size()+1];
        int ss[]=new int[r.size()+1];
        s[0]=0;
        ss[0]=0;
        for(int i=0;i<w.size();i++)
            s[i+1]=s[i]+w.get(w.size()-i-1);
        for(int i=0;i<r.size();i++)
            ss[i+1]=ss[i]+r.get(r.size()-i-1);
        int res=-1;
        int co=0;
        for(int i=0;i<=o.size() && i<k;i++)
        {
            if(i!=0){
            if(k-i<s.length)
                res=Math.max(res, co+s[k-i]);
            if(k-i<ss.length)
                res=Math.max(res, co+ss[k-i]);
            }
            if(i<o.size())
                co=co+o.get(o.size()-1-i);
        }
        System.out.println(res);
    }
}

import java.util.Scanner;
import java.util.Collections;
import java.util.Arrays;
public class project{

    static int max(int a, int b){
        if(a < b)
            return b;
        return a;
    }

    public static void main(String[]args){

    	Scanner s = new Scanner(System.in);

    	int n = s.nextInt(), k = s.nextInt();

    	int a[] = new int[n];

    	for(int i = 0; i < n; i++)
    	    a[i] = s.nextInt();

    	s.nextLine();
    	String[] str;
    	str = s.nextLine().split("");

    	int w[] = new int[n],
    	    o[] = new int[n],
    	    r[] = new int[n];
    	Arrays.fill(w, 0);
    	Arrays.fill(o, 0);
    	Arrays.fill(r, 0);

    	int wsz = 0, osz = 0, rsz = 0;

    	for(int i = 0; i < n; i++){
    	    if(str[i].equals("W"))
    	        w[wsz++] = -a[i];
    	    else if(str[i].equals("O"))
    	        o[osz++] = -a[i];
    	    else if(str[i].equals("R"))
    	        r[rsz++] = -a[i];
    	    else
    	        System.out.println(str[i]);
    	}
    	int maxscore = -1;

    	Arrays.sort(w);
    	Arrays.sort(r);
    	Arrays.sort(o);

        for(int i = 0; i < n; i++){
            w[i] = -w[i];
            r[i] = -r[i];
            o[i] = -o[i];
        }
        for(int i = 1; i < n; i++){
            w[i] += w[i - 1];
            r[i] += r[i - 1];
            o[i] += o[i - 1];
        }

        if(osz != 0 && rsz != 0 && rsz + osz >= k){
            for(int i = 0; i < rsz; i++){
                int j = k - i - 2;
                if(j >= 0 && j < osz)
                    maxscore = max(maxscore, r[i] + o[j]);
            }
        }
        if(osz != 0 && wsz != 0 && wsz + osz >= k){
            for(int i = 0; i < wsz; i++){
                int j = k - i - 2;
                if(j >= 0 && j < osz)
                    maxscore = max(maxscore, w[i] + o[j]);
            }
        }

	    System.out.println(maxscore);
    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        static final int MAX_COORD = (int) 1e5 + 10;
        static final int INF = (int) 1e9;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            int[] b = new int[4 * MAX_COORD];
            int[] orange = new int[4 * MAX_COORD];
            int[] white = new int[4 * MAX_COORD];
            int[] red = new int[4 * MAX_COORD];
            int[] pro = new int[4 * MAX_COORD];
            int[] prr = new int[4 * MAX_COORD];
            int[] prw = new int[4 * MAX_COORD];
            for (int i = 0; i < 4 * MAX_COORD; i++) {
                orange[i] = 0;
                red[i] = 0;
                white[i] = 0;
            }
            int szo = 0, szw = 0, szr = 0;
            String s;
            for (int i = 0; i < n; ++i) {
                b[i] = in.nextInt();
            }
            s = in.next();
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) == 'R') {
                    red[szr++] = b[i];
                }
                if (s.charAt(i) == 'O') {
                    orange[szo++] = b[i];
                }
                if (s.charAt(i) == 'W') {
                    white[szw++] = b[i];
                }
            }
            Arrays.sort(orange);
            Arrays.sort(red);
            Arrays.sort(white);

            for(int i = 0; i < orange.length / 2; i++)
            {
                int temp = orange[i];
                orange[i] = orange[orange.length - i -1];
                orange[orange.length - i - 1] = temp;
            }
            for(int i = 0; i < red.length / 2; i++)
            {
                int temp = red[i];
                red[i] = red[red.length - i -1];
                red[red.length - i - 1] = temp;
            }
            for(int i = 0; i < white.length / 2; i++)
            {
                int temp = white[i];
                white[i] = white[white.length - i -1];
                white[white.length - i - 1] = temp;
            }

            pro[0] = orange[0];
            prw[0] = white[0];
            prr[0] = red[0];
            for (int i = 1; i < n; i++) {
                if (i < szo) {
                    pro[i] = pro[i - 1] + orange[i];
                }
                if (i < szr) {
                    prr[i] = prr[i - 1] + red[i];
                }
                if (i < szw) {
                    prw[i] = prw[i - 1] + white[i];
                }
            }

            int ans = -1;
            if (szo > 0 && szr > 0) {
                for (int i = 1; i < k; i++) {
                    int j = k - i;
                    if (i - 1 < szo && j - 1 < szr) {
                        ans = Math.max(ans, pro[i - 1] + prr[j - 1]);
                    }
                }
            }
            if (szw > 0 && szo > 0) {
                for (int i = 1; i < k; i++) {
                    int j = k - i;
                    if (i - 1 < szo && j - 1 < szw) {
                        ans = Math.max(ans, pro[i - 1] + prw[j - 1]);
                    }
                }
            }

            out.println(ans);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

import java.io.*;
import java.util.*;

public class temp
{
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int k=in.nextInt();
        int[] b=new int[n];
        for(int i=0;i<n;i++)
            b[i]=in.nextInt();
        char[] t=in.next().toCharArray();
        ArrayList<Integer> w=new ArrayList<Integer>();
        ArrayList<Integer> o=new ArrayList<Integer>();
        ArrayList<Integer> r=new ArrayList<Integer>();
        for(int i=0;i<n;i++)
        {
            if(t[i]=='R')
                r.add(b[i]);
            else if(t[i]=='O')
                o.add(b[i]);
            else
                w.add(b[i]);
        }
        Collections.sort(w);
        Collections.sort(o);
        Collections.sort(r);
        int sumw[]=new int[w.size()+1];
        int sumr[]=new int[r.size()+1];
        sumw[0]=0;
        sumr[0]=0;
        for(int i=0;i<w.size();i++)
            sumw[i+1]=sumw[i]+w.get(w.size()-i-1);
        for(int i=0;i<r.size();i++)
            sumr[i+1]=sumr[i]+r.get(r.size()-i-1);
        int res=-1;
        int co=0;
        for(int i=0;i<=o.size() && i<k;i++)
        {
            if(i!=0){
            if(k-i<sumw.length)
                res=Math.max(res, co+sumw[k-i]);
            if(k-i<sumr.length)
                res=Math.max(res, co+sumr[k-i]);
            }
            if(i<o.size())
                co=co+o.get(o.size()-1-i);
        }
        System.out.println(res);
    }
}

import java.util.*;

public class Sol{
	public static void main(String[] argc){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] v = new int[n];
		for (int i = 0; i < n; ++i) v[i] = sc.nextInt();
		String s = sc.next();
		ArrayList<Integer> r = new ArrayList<>();
		ArrayList<Integer> w = new ArrayList<>();
		ArrayList<Integer> o = new ArrayList<>();
		for (int i = 0; i < n; i++) {
		    if (s.charAt(i) == 'R') {
		        r.add(v[i]);
		    } else if (s.charAt(i) == 'W') {
		        w.add(v[i]);
		    } else o.add(v[i]);
		}
		Collections.sort(r);
		Collections.sort(w);
		Collections.sort(o);
		int res = -1;
		if (k == 1) {
		    System.out.println(res);
		}
		else{
		if (!r.isEmpty() && !o.isEmpty() && r.size() + o.size() >= k) {
		    int t = o.get(o.size() - 1) + r.get(r.size() - 1);
		    ArrayList<Integer> al = new ArrayList<>();
		    for (int i = 0; i + 1 < r.size(); ++i)
		        al.add(r.get(i));
		    for (int i = 0; i + 1 < o.size(); ++i)
		        al.add(o.get(i));
		    Collections.sort(al);
		    for (int i = 0; i < k - 2; ++i) t += al.get(al.size() - 1 - i);
		    res = Math.max(res, t);
		}
		if (!w.isEmpty() && !o.isEmpty() && w.size() + o.size() >= k) {
		    int t = o.get(o.size() - 1) + w.get(w.size() - 1);
		    ArrayList<Integer> al = new ArrayList<>();
		    for (int i = 0; i + 1 < w.size(); ++i)
		        al.add(w.get(i));
		    for (int i = 0; i + 1 < o.size(); ++i)
		        al.add(o.get(i));
		    Collections.sort(al);
		    for (int i = 0; i < k - 2;++i) t += al.get(al.size() - 1 - i);
		    res = Math.max(res, t);
		}
		System.out.println(res);
		}
	}
}

import java.util.*;

public class Sol{
	public static void main(String[] argc){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] v = new int[n];
		for (int i = 0; i < n; i++) v[i] = sc.nextInt();
		String s = sc.next();
		ArrayList<Integer> re = new ArrayList<>();
		ArrayList<Integer> wh = new ArrayList<>();
		ArrayList<Integer> or = new ArrayList<>();
		for (int i = 0; i < n; i++) {
		    if (s.charAt(i) == 'R') {
		        re.add(v[i]);
		    } else if (s.charAt(i) == 'W') {
		        wh.add(v[i]);
		    } else or.add(v[i]);
		}
		Collections.sort(re);
		Collections.sort(wh);
		Collections.sort(or);
		int ans = -1;
		if (k == 1) {
		    System.out.println(ans);
		    return;
		}
		if (!re.isEmpty() && !or.isEmpty() && re.size() + or.size() >= k) {
		    int t = or.get(or.size() - 1) + re.get(re.size() - 1);
		    ArrayList<Integer> al = new ArrayList<>();
		    for (int i = 0; i + 1 < re.size(); i++) al.add(re.get(i));
		    for (int i = 0; i + 1 < or.size(); i++) al.add(or.get(i));
		    Collections.sort(al);
		    for (int i = 0; i < k - 2; i++) t += al.get(al.size() - 1 - i);
		    ans = Math.max(ans, t);
		}
		if (!wh.isEmpty() && !or.isEmpty() && wh.size() + or.size() >= k) {
		    int t = or.get(or.size() - 1) + wh.get(wh.size() - 1);
		    ArrayList<Integer> al = new ArrayList<>();
		    for (int i = 0; i + 1 < wh.size(); i++) al.add(wh.get(i));
		    for (int i = 0; i + 1 < or.size(); i++) al.add(or.get(i));
		    Collections.sort(al);
		    for (int i = 0; i < k - 2; i++) t += al.get(al.size() - 1 - i);
		    ans = Math.max(ans, t);
		}
		System.out.println(ans);
	}
}

import java.util.*;

public class Sol{
	public static void main(String[] argc){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), k = sc.nextInt(), amred = 0, amor = 0, amwh = 0;
		Integer[] a = new Integer[n], white = new Integer[n], red = new Integer[n], oran = new Integer[n];
		for(int i = 0; i < n; i++){
			a[i] = sc.nextInt();
			white[i] = 0;
			red[i] = 0;
			oran[i] = 0;
		}
		String s = sc.next();
		for(int i = 0; i < n; i++){
		    if(s.charAt(i) == 'W'){
		        white[amwh++] = a[i];
		    }
		    else if(s.charAt(i) == 'O'){
		        oran[amor++] = a[i];
		    }
		    else{
		        red[amred++] = a[i];
		    }
		}
		int res = -1;
		Arrays.sort(red, Collections.reverseOrder());
		Arrays.sort(oran, Collections.reverseOrder());
		Arrays.sort(white, Collections.reverseOrder());
		for(int i = 1; i < amred; i++){
		    red[i] += red[i-1];
		}
		for(int i = 1; i < amor; i++){
		    oran[i] += oran[i-1];
		}
		for(int i = 1; i < amwh; i++){
		    white[i] += white[i-1];
		}
        for(int i = 1; i < k; i++){
            if(i <= amred && k-i <= amor){
                res = Math.max(res, red[i-1]+oran[k-i-1]);
            }
        }
        for(int i = 1; i < k; i++){
            if(i <= amor && k-i <= amwh){
                res = Math.max(res, oran[i-1]+white[k-i-1]);
            }
        }
        System.out.println(res);
	}
}

import java.util.*;
public class Task {

    public static long max(long a, long b ) {
        if(a < b) {
            return b;
        }
        return a;
    }

    public static int max(int a, int b ) {
        if(a < b) {
            return b;
        }
        return a;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] beauty = new int[200005];
        for(int i = 0; i < n; i++) {
            beauty[i] = sc.nextInt();
        }
        sc.nextLine();
        String str = sc.nextLine();
        int rcn = 0;
        int wcn = 0;
        int ocn = 0;
        int rsum = 0;
        int wsum = 0;
        int osum = 0;
        ArrayList<Integer> wo = new ArrayList<Integer>();
        ArrayList<Integer> ro = new ArrayList<Integer>();
        int maxo = -1;
        int maxr = -1;
        int maxw = -1;
        for(int i = 0; i < n; i++) {
            if(str.charAt(i) == 'R') {
                maxr = max(maxr,beauty[i]);
                rcn++;
                ro.add(beauty[i]);
                rsum+= beauty[i];
            }
            if(str.charAt(i) == 'W') {
                maxw = max(maxw,beauty[i]);
                wcn++;
                wo.add(beauty[i]);
                wsum+= beauty[i];
            }
            if(str.charAt(i) == 'O') {
                maxo = max(maxo,beauty[i]);
                ocn++;
                ro.add(beauty[i]);
                wo.add(beauty[i]);
                osum+= beauty[i];
            }
        }
        if(k == 1 || ocn == 0 || max(ocn+wcn,ocn+rcn) < k || (rcn == 0 && wcn == 0)) {
            System.out.print(-1);
        } else {
            if(rcn > 0) {
                ro.remove(ro.indexOf(maxr));
            }
            ro.remove(ro.indexOf(maxo));
            if(wcn > 0) {
                wo.remove(wo.indexOf(maxw));
            }
            wo.remove(wo.indexOf(maxo));
            Collections.sort(ro);
            Collections.sort(wo);
            long ans1 = maxr+maxo;
            long ans2 = maxw+maxo;
            if(ro.size()+2 >= k) {
                for(int i = ro.size()-1; i >= ro.size()-k+2; i--) {
                    ans1+= ro.get(i);
                }
            }
            if(wo.size()+2 >= k) {
                for(int i = wo.size()-1; i >= wo.size()-k+2; i--) {
                    ans2+= wo.get(i);
                }
            }
            System.out.print(max(ans1,ans2));
        }
    }
}

import java.io.*;
import java.util.*;

public class VK {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        InputReader in = new InputReader(System.in);

        int n = in.nextInt();
        int k = in.nextInt();

        int[] b = new int[n];
        for(int i = 0; i < n; i++) {
            b[i] = in.nextInt();
        }

        String s = in.next();

        if(k == 1) {
            out.println("-1");
            out.close();
            return;
        }

        ArrayList<Integer> red = new ArrayList<>();
        ArrayList<Integer> orange = new ArrayList<>();
        ArrayList<Integer> white = new ArrayList<>();

        for(int i = 0; i < n; i++) {
            switch (s.charAt(i)) {
                case 'R':
                    red.add(b[i]);
                    break;
                case 'O':
                    orange.add(b[i]);
                    break;
                case 'W':
                    white.add(b[i]);
                    break;
            }
        }
        orange.add(Integer.MIN_VALUE);
        red.add(Integer.MIN_VALUE);
        white.add(Integer.MIN_VALUE);

        Collections.sort(red);
        Collections.sort(orange);
        Collections.sort(white);
        ArrayList<Integer> copy = new ArrayList<>(orange);

        int r = -1;
        int w = -1;

        if(orange.size() > 1 && red.size() > 1 && orange.size() + red.size() >= k + 2) {
            r = orange.remove(orange.size() - 1);
            r += red.remove(red.size() - 1);
            for (int i = 2; i < k; i++) {
                int rm = red.get(red.size() - 1);
                int om = orange.get(orange.size() - 1);
                if (rm > om) {
                    red.remove(red.size() - 1);
                    r += rm;
                } else {
                    orange.remove(orange.size() - 1);
                    r += om;
                }
            }
        }

        orange = copy;
        if(orange.size() > 1 && white.size() > 1 && orange.size() + white.size() >= k + 2) {
            w = orange.remove(orange.size() - 1);
            w += white.remove(white.size() - 1);
            for (int i = 2; i < k; i++) {
                int wm = white.get(white.size() - 1);
                int om = orange.get(orange.size() - 1);
                if (wm > om) {
                    white.remove(white.size() - 1);
                    w += wm;
                } else {
                    orange.remove(orange.size() - 1);
                    w += om;
                }
            }
        }

        out.println(Math.max(r, w));
        out.close();
    }

    static int nod(int a, int b) {
        while(b > 0) {
            int k = a % b;
            a = b;
            b = k;
        }

        return a;
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {return Long.parseLong(next());}
    }
}

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {

	static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer st = new StringTokenizer("");
	static PrintWriter out = new PrintWriter(System.out);

	static String next() throws Exception {
		while(!st.hasMoreTokens()) {
			st = new StringTokenizer(bf.readLine());
		}
		return st.nextToken();
	}

	static int nextInt() throws Exception {
		return Integer.parseInt(next());
	}

	static long nextLong() throws Exception {
		return Long.parseLong(next());
	}

	static class Solver {
	    int k;
	    int get(ArrayList<Integer> r, ArrayList<Integer> o) {
	        int pr = 0, po = 0;
            int sum = 0;
            if (k == 1) {
                return 0;
            }
            if (r.size() != 0 && o.size() != 0) {
                if (r.size() + o.size() >= k) {
                    for (int i = 0; i < k - 1; i++) {
                        if (pr != r.size() && po != o.size()) {
                            if (r.get(pr) < o.get(po)) {
                                sum += o.get(po++);
                            }
                            else {
                                sum += r.get(pr++);
                            }
                        }
                        else if (pr != r.size()) {
                            sum += r.get(pr++);
                        }
                        else {
                            sum += o.get(po++);
                        }
                    }
                    if (pr == 0) {
                        sum += r.get(0);
                    }
                    else if (po == 0) {
                        sum += o.get(0);
                    }
                    else {
                        if (pr != r.size() && po != o.size()) {
                            if (r.get(pr) < o.get(po)) {
                                sum += o.get(po++);
                            }
                            else {
                                sum += r.get(pr++);
                            }
                        }
                        else if (pr != r.size()) {
                            sum += r.get(pr++);
                        }
                        else {
                            sum += o.get(po++);
                        }
                    }
                }
            }
            return sum;
	    }

		public void solve() throws Exception {
			int n = nextInt();
			k = nextInt();
			ArrayList<Integer> r = new ArrayList<Integer>();
			ArrayList<Integer> o = new ArrayList<Integer>();
			ArrayList<Integer> w = new ArrayList<Integer>();
			int[] t = new int[n];
			for (int i = 0; i < n; i++) {
			    t[i] = nextInt();
			}
			String s = next();
			for (int i = 0; i < n; i++) {
			    if (s.charAt(i) == 'R') {
			        r.add(t[i]);
			    }
			    else if (s.charAt(i) == 'O') {
			        o.add(t[i]);
			    }
			    else {
			        w.add(t[i]);
			    }
			}
			Collections.sort(r, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    return -(o1.compareTo(o2));
                }
            });
            Collections.sort(o, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    return -(o1.compareTo(o2));
                }
            });
            Collections.sort(w, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    return -(o1.compareTo(o2));
                }
            });
            int ans = Math.max(get(r, o), get(w, o));
            if (ans == 0) {
                out.println(-1);
            }
            else {
                out.println(ans);
            }
            out.flush();
		}
	}

	public static void main(String[] args) throws Exception {
		(new Solver()).solve();
	}
}

import java.lang.reflect.Array;
import java.util.*;

public class Main {

    static int solve(ArrayList<Integer> a, ArrayList<Integer> b, int k) {
        if (a.size() + b.size()<k)return -1;
        if (a.size()==0)return -1;
        if (b.size()==0)return -1;
        if (k < 2) return -1;
        int res = a.get(0) + b.get(0);
        int i = 1;
        int j = 1;
        k-=2;
        while (k > 0) {
            if (i==a.size()) {
                res += b.get(j);
                j++;
            } else if (j==b.size()) {
                res += a.get(i);
                i++;
            } else {
                if (a.get(i) > b.get(j)) {
                    res += a.get(i);
                    i++;
                } else {
                    res += b.get(j);
                    j++;
                }
            }
            k--;
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        ArrayList<Integer> white = new ArrayList<>();
        ArrayList<Integer> red = new ArrayList<>();
        ArrayList<Integer> orange = new ArrayList<>();
        int[] b = new int[n];

        for (int i = 0; i < n; i++) {
            b[i] = in.nextInt();
        }

        String s = in.next();

        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == 'R') red.add(b[i]);
            if (s.charAt(i) == 'O') orange.add(b[i]);
            if (s.charAt(i) == 'W') white.add(b[i]);
        }

        Collections.sort(white, (o1, o2) -> o2 - o1);
        Collections.sort(red, (o1, o2) -> o2 - o1);
        Collections.sort(orange, (o1, o2) -> o2 - o1);

        int res = Math.max(solve(orange, red, k), solve(orange, white, k));

        System.out.println(res);
        in.close();
    }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class A {
	public static BufferedReader reader;
	public static PrintWriter out;
	public static StringTokenizer tokenizer;

	public static String nextToken() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
			}
		}
		return tokenizer.nextToken();
	}

	public static int nextInt() {
		return Integer.parseInt(nextToken());
	}

	public static long nextLong() {
		return Long.parseLong(nextToken());
	}

	public static int[] nextIntArray(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public static int getSum(long i) {
		return getStrSum(i + "");
	}

	public static int getStrSum(String n) {
		int sum = 0;
		for (int j = 0; j < n.length(); j++) {
			sum += (n.charAt(j) - '0');
		}
		return sum;
	}

	public static void solve() {
		int n = nextInt(), k = nextInt();
		int[] b = nextIntArray(n);
		char[] cols = nextToken().toCharArray();
		int wAm = 0, oAm = 0, rAm = 0;
		for(int i = 0; i < n; i++) {
			char c = cols[i];
			if (c == 'W') {
				wAm++;
			}
			if (c == 'O') {
				oAm++;
			}
			if (c == 'R') {
				rAm++;
			}
		}
		int[] ws = new int[wAm];
		int[] rs = new int[rAm];
		int[] os = new int[oAm];
		int wi = 0, ri = 0, oi = 0;
		for(int i = 0; i < n; i++) {
			char c = cols[i];
			if (c == 'W') {
				ws[wi] = b[i];
				wi++;
			}
			if (c == 'O') {
				os[oi] = b[i];
				oi++;
			}
			if (c == 'R') {
				rs[ri] = b[i];
				ri++;
			}
		}
		Arrays.sort(ws);
		Arrays.sort(rs);
		Arrays.sort(os);
		if (oAm == 0 || (rAm + wAm == 0) || k > oAm + Math.max(wAm, rAm) || k == 1) {
			out.println(-1);
		} else {
			int first = 0, second = 0;

			if (k <= oAm + rAm && rAm != 0) {
				oi = os.length - 1;
				ri = rs.length - 1;
				first += (os[oi] + rs[ri]);
				oi--;
				ri--;
				for (int i = 0; i < k - 2; i++) {
					if (ri == -1 || (oi != -1 && os[oi] > rs[ri])) {
						first += os[oi];
						oi--;
					} else {
						first += rs[ri];
						ri--;
					}
				}
			}

			if (k <= oAm + wAm && wAm != 0) {
				oi = os.length - 1;
				wi = ws.length - 1;
				second += (os[oi] + ws[wi]);
				oi--;
				wi--;
				for (int i = 0; i < k - 2; i++) {
					if (wi == -1 || (oi != -1 && os[oi] > ws[wi])) {
						second += os[oi];
						oi--;
					} else {
						second += ws[wi];
						wi--;
					}
				}
			}

			out.println(Math.max(first, second));
		}
	}

	public static void main(String[] args) throws IOException {
		reader = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		reader.close();
		out.close();
	}
}

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

//        Scanner sc = new Scanner(new File("src/input.in"));
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int k = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }

        ArrayList<Integer> red = new ArrayList<>();
        ArrayList<Integer> orange = new ArrayList<>();
        ArrayList<Integer> white = new ArrayList<>();

        String s = sc.nextLine();
        s = sc.nextLine();
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            int x = list.get(i);
            if (c == 'R') {
                red.add(x);
            } else if (c == 'O') {
                orange.add(x);
            } else {
                white.add(x);
            }
        }

        if (k <= 1) {
            System.out.println(-1);
            return;
        }
        Collections.sort(orange);
        Collections.sort(white);
        Collections.sort(red);

        if (orange.size() == 0) {
            System.out.println(-1);
            return;
        }

        int mo;
        int mw;
        int mr;

        if (red.size() == 0) {
            mr = Integer.MIN_VALUE;
        } else {
            mr = red.get(red.size() - 1);
            red.remove(red.size() - 1);
        }

        if (white.size() == 0) {
            mw = Integer.MIN_VALUE;
        } else {
            mw = white.get(white.size() - 1);
            white.remove(white.size() - 1);
        }

        mo = orange.get(orange.size() - 1);
        orange.remove(orange.size() - 1);

        ArrayList<Integer> or = new ArrayList<>(orange);
        ArrayList<Integer> ow = new ArrayList<>(orange);

        or.addAll(red);
        Collections.sort(or);

        ow.addAll(white);
        Collections.sort(ow);

        int sr = mo + mr;
        for (int i = 0; i < k - 2 && i < or.size(); i++) {
            sr += or.get(or.size() - i - 1);
        }

        int sw = mo + mw;
        for (int i = 0; i < k - 2 && i < ow.size(); i++) {
            sw += ow.get(ow.size() - i - 1);
        }

        if (or.size() < k - 2 && ow.size() < k - 2) {
            System.out.println(-1);
            return;
        }

        if (or.size() < k - 2) {
            System.out.println(sw);
            return;
        }

        if (ow.size() < k - 2) {
            System.out.println(sr);
            return;
        }
        int ans = Math.max(sr, sw);
        if (ans >= 0) {
            System.out.println(ans);
        } else {
            System.out.println(-1);
        }
    }

}

import java.util.*;
import java.io.*;

public class Main{
    private final static String[] local = {"5 3\n" +
            "4 3 4 1 6\n" +
            "RROWW","5 2\n" +
            "10 20 14 20 11\n" +
            "RRRRR","11 5\n" +
            "5 6 3 2 3 4 7 5 4 5 6\n" +
            "RWOORWORROW","",""};

    static class Task {

        private void solve() {
            int n = ni(), k = ni();
            int[] b = na(n);
            char[] input = ns().toCharArray();
            int cnt1 = 0, cnt2 = 0, cnt3 = 0;
            ArrayList<Integer> list1 = new ArrayList<>();
            ArrayList<Integer> list3 = new ArrayList<>();
            int max0 = 0, maxr = 0, maxw = 0;
            for (int i = 0; i < n; ++i) {
                char c = input[i];
                if (c == 'R') {
                    cnt1++;
                    list1.add(b[i]);
                    maxr = Math.max(maxr, b[i]);
                }
                else if (c == 'O') {
                    cnt2++;
                    max0 = Math.max(b[i], max0);
                }
                else {
                    cnt3++;
                    list3.add(b[i]);
                    maxw = Math.max(maxw, b[i]);
                }
            }
            if (cnt2 == 0 || ((cnt1 + cnt2 < k) && (cnt2 + cnt3) < k) || (cnt1 == 0 && cnt3 == 0) || k <= 1 ) {
                out.println(-1);
                return;
            }
            for (int i = 0; i < list1.size(); ++i) {
                if (list1.get(i) == maxr) {
                    list1.remove(i);
                    break;
                }
            }
            for (int i = 0; i < list3.size(); ++i) {
                if (list3.get(i) == maxw) {
                    list3.remove(i);
                    break;
                }
            }
            debug(list1);
            debug(list3);
            boolean flag = false;
            for (int i = 0; i < n; ++i) {
                if (input[i] != 'O') continue;
                if (b[i] == max0 && !flag) {
                    flag = true;
                } else {
                    list1.add(b[i]);
                    list3.add(b[i]);
                }
            }
            debug(maxr);
            debug(maxw);
            debug(list1);
            debug(list3);
            if (cnt1 + cnt2 >= k && cnt2 + cnt3 >= k) {
                out.println(Math.max(get(list1, max0 + maxr, k - 2), get(list3, max0 + maxw, k - 2)));
            } else if (cnt1 + cnt2 >= k) {
                out.println(get(list1, max0 + maxr, k - 2));
            } else {
                out.println(get(list3, max0 + maxw, k - 2));
            }
        }

        int get(ArrayList<Integer> list, int init, int k) {
            int ans = init;
            Collections.sort(list);
            Collections.reverse(list);
            Iterator<Integer> it1 = list.iterator();
            for (int i = 0; i < k; ++i)
                ans += it1.next();
            return ans;
        }

        void run(String INPUT, boolean oj){this.oj=oj;out=new PrintWriter(System.out);inputStream = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());long startTime=System.currentTimeMillis();solve();out.flush();long endTime=System.currentTimeMillis();debug(endTime - startTime + "ms");}
        private InputStream inputStream;private PrintWriter out;private boolean oj;private byte[] inbuf=new byte[1024];private int lenbuf=0,ptrbuf=0;
        private int readByte(){if(lenbuf == -1)throw new RuntimeException();if(ptrbuf>=lenbuf){ptrbuf=0;try{lenbuf=inputStream.read(inbuf);}catch(IOException e){throw new RuntimeException();}if(lenbuf<=0) return -1;}return inbuf[ptrbuf++]; }
        private boolean isSpaceChar(int c){return !(c>=33&&c<=126);}private int skip(){int b;while((b=readByte())!=-1&&isSpaceChar(b));return b;}
        private int[] na(int n){int[] a = new int[n]; for(int i = 0; i < n; ++i) a[i] = ni(); return a;}
        private double nd(){return Double.parseDouble(ns());}private char nc(){return (char)skip();}private int ni(){return Integer.parseInt(ns());}private long nl(){return Long.parseLong(ns());}
        private String ns(){int b = skip();StringBuilder sb = new StringBuilder();while (!(isSpaceChar(b))) {sb.appendCodePoint(b);b = readByte();}return sb.toString(); }
        private int debugCnt = 0;private void debug(Object... o){if(!oj) System.out.println("debug:"+ ++debugCnt + " " + Arrays.deepToString(o));}
    }
    public static void main(String[] args){
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        //boolean oj = true;
        if(oj)new Task().run(null, true);else{int test = 1;for(String aLocal : local){if(aLocal.equals(""))continue;System.out.println("Test" + test++ + ":");new Task().run(aLocal,false);}}
    }
}

import java.io.BufferedReader;
// import java.io.FileInputStream;
// import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;

import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.copyOf;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
import static java.util.Collections.reverse;
import static java.util.Collections.reverseOrder;
import static java.util.Collections.sort;

public class Main {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        int n = in.nextInt(), k = in.nextInt();

        long[] p = new long[n];
        for (int i = 0; i < n; i++)
            p[i] = in.nextLong();

        ArrayList<Long> w = new ArrayList<>();
        ArrayList<Long> o = new ArrayList<>();
        ArrayList<Long> r = new ArrayList<>();

        char[] s = in.next().toCharArray();
        for (int i = 0; i < n; i++) {
            if (s[i] == 'R')
                r.add(p[i]);
            if (s[i] == 'O')
                o.add(p[i]);
            if (s[i] == 'W')
                w.add(p[i]);
        }

        sort(r, reverseOrder());
        sort(o, reverseOrder());
        sort(w, reverseOrder());

        for (int i = 1; i < r.size(); i++) {
            r.set(i, r.get(i) + r.get(i - 1));
        }

        for (int i = 1; i < o.size(); i++) {
            o.set(i, o.get(i) + o.get(i - 1));
        }

        for (int i = 1; i < w.size(); i++) {
            w.set(i, w.get(i) + w.get(i - 1));
        }

        long ans = -1;

        for (int i = 1, j = k - 1; i < k; i++, j--) {
            if (i <= r.size() && j <= o.size())
                ans = max(ans, r.get(i - 1) + o.get(j - 1));
        }

        for (int i = 1, j = k - 1; i < k; i++, j--) {
            if (i <= w.size() && j <= o.size())
                ans = max(ans, w.get(i - 1) + o.get(j - 1));
        }

        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next().replace(',', '.'));
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}

import java.io.*;
import java.util.*;

public class Main {

    private static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException exp) {
                    throw new RuntimeException(exp);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }
    }

    public static void main(String[] args) {    //throws FileNotFoundException {
        InputStream inputStream = System.in;    //new FileInputStream("input.txt");
        OutputStream outputStream = System.out; //new FileOutputStream("output.txt");
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        Task newTask = new Task();
        newTask.solve(in, out);

        out.close();
    }

    private static class Task {
        //.constant
        private final int INF = Integer.MAX_VALUE;
        private final int MOD = (int)1e9 + 7;

        //.data
        class Rose implements Comparable<Rose> {
            public int b = 0;
            public char c = 0;
            public int compareTo(Rose other) {
                return -Integer.compare(b, other.b);
            }
        }

        //.code
        public void solve(InputReader in, PrintWriter out) {
            int n = in.nextInt();
            Rose[] rose = new Rose[n];
            int k = in.nextInt();
            for (int i = 0; i < n; ++i) {
                rose[i] = new Rose();
                rose[i].b = in.nextInt();
            }
            String s = in.next();
            for (int i = 0; i < n; ++i) {
                rose[i].c = s.charAt(i);
            }
            Arrays.sort(rose);

            int ans = -1;
            int cntO = 0;
            int cntD = 0;
            int sum = 0;
            for (Rose r : rose) {
                if (r.c != 'W') {
                    if (r.c == 'O' && cntO != k - 1) {
                        ++cntO;
                        sum += r.b;
                    } else if (cntD != k - 1) {
                        ++cntD;
                        sum += r.b;
                    }
                }
                if (cntO + cntD == k) {
                    ans = sum;
                    break;
                }
            }
            cntO = 0;
            cntD = 0;
            sum = 0;
            for (Rose r : rose) {
                if (r.c != 'R') {
                    if (r.c == 'O' && cntO != k - 1) {
                        ++cntO;
                        sum += r.b;
                    } else if (cntD != k - 1) {
                        ++cntD;
                        sum += r.b;
                    }
                }
                if (cntO + cntD == k) {
                    ans = sum;
                    break;
                }
            }

            out.println(ans);

        }//end

    }

}

import java.io.*;
import java.util.*;

public class Main {

    private static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException exp) {
                    throw new RuntimeException(exp);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }
    }

    public static void main(String[] args) {    //throws FileNotFoundException {
        InputStream inputStream = System.in;    //new FileInputStream("input.txt");
        OutputStream outputStream = System.out; //new FileOutputStream("output.txt");
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        Task newTask = new Task();
        newTask.solve(in, out);

        out.close();
    }

    private static class Task {
        //.constant
        private final int INF = Integer.MAX_VALUE;
        private final int MOD = (int)1e9 + 7;

        //.data
        class Rose implements Comparable<Rose> {
            public int b = 0;
            public char c = 0;
            public int compareTo(Rose other) {
                return -Integer.compare(b, other.b);
            }
        }

        //.code
        public void solve(InputReader in, PrintWriter out) {
            int n = in.nextInt();
            Rose[] rose = new Rose[n];
            int k = in.nextInt();
            for (int i = 0; i < n; ++i) {
                rose[i] = new Rose();
                rose[i].b = in.nextInt();
            }
            String s = in.next();
            for (int i = 0; i < n; ++i) {
                rose[i].c = s.charAt(i);
            }
            Arrays.sort(rose);

            int ans = -1;
            int cntO = 0;
            int cntD = 0;
            int sum = 0;
            for (Rose r : rose) {
                if (r.c != 'W') {
                    if (r.c == 'O' && cntO != k - 1) {
                        ++cntO;
                        sum += r.b;
                    } else if (cntD != k - 1) {
                        ++cntD;
                        sum += r.b;
                    }
                }
                if (cntO + cntD == k) {
                    ans = sum;
                    break;
                }
            }
            cntO = 0;
            cntD = 0;
            sum = 0;
            for (Rose r : rose) {
                if (r.c != 'R') {
                    if (r.c == 'O' && cntO != k - 1) {
                        ++cntO;
                        sum += r.b;
                    } else if (cntD != k - 1) {
                        ++cntD;
                        sum += r.b;
                    }
                }
                if (cntO + cntD == k) {
                    ans = Math.max(ans, sum);
                    break;
                }
            }

            out.println(ans);

        }//end

    }

}

import java.util.*;
public class Task {

    public static int max(int a, int b ) {
        if(a < b) {
            return b;
        }
        return a;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] beauty = new int[200005];
        for(int i = 0; i < n; i++) {
            beauty[i] = sc.nextInt();
        }
        sc.nextLine();
        String str = sc.nextLine();
        int rcn = 0;
        int wcn = 0;
        int ocn = 0;
        int rsum = 0;
        int wsum = 0;
        int osum = 0;
        ArrayList<Integer> wo = new ArrayList<Integer>();
        ArrayList<Integer> ro = new ArrayList<Integer>();
        for(int i = 0; i < n; i++) {
            if(str.charAt(i) == 'R') {
                rcn++;
                ro.add(beauty[i]);
                rsum+= beauty[i];
            }
            if(str.charAt(i) == 'W') {
                wcn++;
                wo.add(beauty[i]);
                wsum+= beauty[i];
            }
            if(str.charAt(i) == 'O') {
                ocn++;
                ro.add(beauty[i]);
                wo.add(beauty[i]);
                osum+= beauty[i];
            }
        }
        if(ocn == 0 || max(ocn+wcn,ocn+rcn) < k) {
            System.out.print(-1);
        } else {
            Collections.sort(ro);
            Collections.sort(wo);
            int ans1 = 0;
            int ans2 = 0;
            if(ro.size() >= k) {
                for(int i = ro.size()-1; i >= ro.size()-k; i--) {
                    ans1+= ro.get(i);
                }
            }
            if(wo.size() >= k) {
                for(int i = wo.size()-1; i >= wo.size()-k; i--) {
                    ans2+= wo.get(i);
                }
            }
            System.out.print(max(ans1,ans2));
        }
    }
}

import java.util.*;
public class Task {

    public static long max(long a, long b ) {
        if(a < b) {
            return b;
        }
        return a;
    }

    public static int max(int a, int b ) {
        if(a < b) {
            return b;
        }
        return a;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] beauty = new int[200005];
        for(int i = 0; i < n; i++) {
            beauty[i] = sc.nextInt();
        }
        sc.nextLine();
        String str = sc.nextLine();
        int rcn = 0;
        int wcn = 0;
        int ocn = 0;
        int rsum = 0;
        int wsum = 0;
        int osum = 0;
        ArrayList<Integer> wo = new ArrayList<Integer>();
        ArrayList<Integer> ro = new ArrayList<Integer>();
        int maxo = 0;
        int maxr = 0;
        int maxw = 0;
        for(int i = 0; i < n; i++) {
            if(str.charAt(i) == 'R') {
                maxr = max(maxr,beauty[i]);
                rcn++;
                ro.add(beauty[i]);
                rsum+= beauty[i];
            }
            if(str.charAt(i) == 'W') {
                maxw = max(maxw,beauty[i]);
                wcn++;
                wo.add(beauty[i]);
                wsum+= beauty[i];
            }
            if(str.charAt(i) == 'O') {
                maxo = max(maxo,beauty[i]);
                ocn++;
                ro.add(beauty[i]);
                wo.add(beauty[i]);
                osum+= beauty[i];
            }
        }
        if(k == 1 || ocn == 0 || max(ocn+wcn,ocn+rcn) < k) {
            System.out.print(-1);
        } else {
            Collections.sort(ro);
            Collections.sort(wo);
            long ans1 = 0;
            long ans2 = 0;
            if(ro.size() >= k) {
                for(int i = ro.size()-1; i >= ro.size()-k; i--) {
                    ans1+= ro.get(i);
                }
                if(ro.get(ro.size()-k) > maxo) {
                    ans1-= ro.get(ro.size()-k);
                    ans1+= maxo;
                }
                if(ro.get(ro.size()-k) > maxr) {
                    ans1-= ro.get(ro.size()-k);
                    ans1+= maxr;
                }
            }
            if(wo.size() >= k) {
                for(int i = wo.size()-1; i >= wo.size()-k; i--) {
                    ans2+= wo.get(i);
                }
                if(wo.get(wo.size()-k) > maxo) {
                    ans2-= wo.get(wo.size()-k);
                    ans2+= maxo;
                }
                if(wo.get(wo.size()-k) > maxw) {
                    ans2-= wo.get(wo.size()-k);
                    ans2+= maxw;
                }
            }
            System.out.print(max(ans1,ans2));
        }
    }
}

import java.util.*;
public class Task {

    public static long max(long a, long b ) {
        if(a < b) {
            return b;
        }
        return a;
    }

    public static int max(int a, int b ) {
        if(a < b) {
            return b;
        }
        return a;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] beauty = new int[200005];
        for(int i = 0; i < n; i++) {
            beauty[i] = sc.nextInt();
        }
        sc.nextLine();
        String str = sc.nextLine();
        int rcn = 0;
        int wcn = 0;
        int ocn = 0;
        int rsum = 0;
        int wsum = 0;
        int osum = 0;
        ArrayList<Integer> wo = new ArrayList<Integer>();
        ArrayList<Integer> ro = new ArrayList<Integer>();
        int maxo = 0;
        for(int i = 0; i < n; i++) {
            if(str.charAt(i) == 'R') {
                rcn++;
                ro.add(beauty[i]);
                rsum+= beauty[i];
            }
            if(str.charAt(i) == 'W') {
                wcn++;
                wo.add(beauty[i]);
                wsum+= beauty[i];
            }
            if(str.charAt(i) == 'O') {
                maxo = max(maxo,beauty[i]);
                ocn++;
                ro.add(beauty[i]);
                wo.add(beauty[i]);
                osum+= beauty[i];
            }
        }
        if(ocn == 0 || max(ocn+wcn,ocn+rcn) < k) {
            System.out.print(-1);
        } else {
            Collections.sort(ro);
            Collections.sort(wo);
            long ans1 = 0;
            long ans2 = 0;
            if(ro.size() >= k) {
                for(int i = ro.size()-1; i >= ro.size()-k; i--) {
                    ans1+= ro.get(i);
                }
                if(ro.get(ro.size()-k) > maxo) {
                    ans1-= ro.get(ro.size()-k);
                    ans1+= maxo;
                }
            }
            if(wo.size() >= k) {
                for(int i = wo.size()-1; i >= wo.size()-k; i--) {
                    ans2+= wo.get(i);
                }
                if(wo.get(wo.size()-k) > maxo) {
                    ans1-= wo.get(wo.size()-k);
                    ans1+= maxo;
                }
            }
            System.out.print(max(ans1,ans2));
        }
    }
}

import java.util.*;
public class Task {

    public static long max(long a, long b ) {
        if(a < b) {
            return b;
        }
        return a;
    }

    public static int max(int a, int b ) {
        if(a < b) {
            return b;
        }
        return a;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] beauty = new int[200005];
        for(int i = 0; i < n; i++) {
            beauty[i] = sc.nextInt();
        }
        sc.nextLine();
        String str = sc.nextLine();
        int rcn = 0;
        int wcn = 0;
        int ocn = 0;
        int rsum = 0;
        int wsum = 0;
        int osum = 0;
        ArrayList<Integer> wo = new ArrayList<Integer>();
        ArrayList<Integer> ro = new ArrayList<Integer>();
        int maxo = 0;
        int maxr = 0;
        int maxw = 0;
        for(int i = 0; i < n; i++) {
            if(str.charAt(i) == 'R') {
                maxr = max(maxr,beauty[i]);
                rcn++;
                ro.add(beauty[i]);
                rsum+= beauty[i];
            }
            if(str.charAt(i) == 'W') {
                maxw = max(maxw,beauty[i]);
                wcn++;
                wo.add(beauty[i]);
                wsum+= beauty[i];
            }
            if(str.charAt(i) == 'O') {
                maxo = max(maxo,beauty[i]);
                ocn++;
                ro.add(beauty[i]);
                wo.add(beauty[i]);
                osum+= beauty[i];
            }
        }
        if(ocn == 0 || max(ocn+wcn,ocn+rcn) < k) {
            System.out.print(-1);
        } else {
            Collections.sort(ro);
            Collections.sort(wo);
            long ans1 = 0;
            long ans2 = 0;
            if(ro.size() >= k) {
                for(int i = ro.size()-1; i >= ro.size()-k; i--) {
                    ans1+= ro.get(i);
                }
                if(ro.get(ro.size()-k) > maxo) {
                    ans1-= ro.get(ro.size()-k);
                    ans1+= maxo;
                }
                if(ro.get(ro.size()-k) > maxr) {
                    ans1-= ro.get(ro.size()-k);
                    ans1+= maxr;
                }
            }
            if(wo.size() >= k) {
                for(int i = wo.size()-1; i >= wo.size()-k; i--) {
                    ans2+= wo.get(i);
                }
                if(wo.get(wo.size()-k) > maxo) {
                    ans2-= wo.get(wo.size()-k);
                    ans2+= maxo;
                }
                if(wo.get(wo.size()-k) > maxw) {
                    ans2-= wo.get(wo.size()-k);
                    ans2+= maxw;
                }
            }
            System.out.print(max(ans1,ans2));
        }
    }
}

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {

	static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer st = new StringTokenizer("");
	static PrintWriter out = new PrintWriter(System.out);

	static String next() throws Exception {
		while(!st.hasMoreTokens()) {
			st = new StringTokenizer(bf.readLine());
		}
		return st.nextToken();
	}

	static int nextInt() throws Exception {
		return Integer.parseInt(next());
	}

	static long nextLong() throws Exception {
		return Long.parseLong(next());
	}

	static class Solver {
	    int k;
	    int get(ArrayList<Integer> r, ArrayList<Integer> o) {
	        int pr = 0, po = 0;
            int sum = 0;
            if (k == 1) {
                return 0;
            }
            if (r.size() != 0 && o.size() != 0) {
                if (r.size() + o.size() >= k) {
                    for (int i = 0; i < k - 1; i++) {
                        if (pr != r.size() && po != o.size()) {
                            if (r.get(pr) < o.get(po)) {
                                sum += o.get(po++);
                            }
                            else {
                                sum += r.get(pr++);
                            }
                        }
                        else if (pr != r.size()) {
                            sum += r.get(pr++);
                        }
                        else {
                            sum += o.get(po++);
                        }
                    }
                    if (pr == 0) {
                        sum += r.get(0);
                    }
                    else if (po == 0) {
                        sum += o.get(0);
                    }
                    else {
                        if (pr != r.size() && po != o.size()) {
                            if (r.get(pr) < o.get(po)) {
                                sum += o.get(po++);
                            }
                            else {
                                sum += r.get(pr++);
                            }
                        }
                        else if (pr != r.size()) {
                            sum += r.get(pr++);
                        }
                        else {
                            sum += o.get(po++);
                        }
                    }
                }
            }
            return sum;
	    }

		public void solve() throws Exception {
			int n = nextInt();
			k = nextInt();
			ArrayList<Integer> r = new ArrayList<Integer>();
			ArrayList<Integer> o = new ArrayList<Integer>();
			ArrayList<Integer> w = new ArrayList<Integer>();
			int[] t = new int[n];
			for (int i = 0; i < n; i++) {
			    t[i] = nextInt();
			}
			String s = next();
			for (int i = 0; i < n; i++) {
			    if (s.charAt(i) == 'R') {
			        r.add(t[i]);
			    }
			    else if (s.charAt(i) == 'O') {
			        o.add(t[i]);
			    }
			    else {
			        w.add(t[i]);
			    }
			}
			Collections.sort(r, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    return -(o1.toString().compareTo(o2.toString()));
                }
            });
            Collections.sort(o, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    return -(o1.toString().compareTo(o2.toString()));
                }
            });
            Collections.sort(w, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    return -(o1.toString().compareTo(o2.toString()));
                }
            });
            int ans = Math.max(get(r, o), get(w, o));
            if (ans == 0) {
                out.println(-1);
            }
            else {
                out.println(ans);
            }
            out.flush();
		}
	}

	public static void main(String[] args) throws Exception {
		(new Solver()).solve();
	}
}

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {

	static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer st = new StringTokenizer("");
	static PrintWriter out = new PrintWriter(System.out);

	static String next() throws Exception {
		while(!st.hasMoreTokens()) {
			st = new StringTokenizer(bf.readLine());
		}
		return st.nextToken();
	}

	static int nextInt() throws Exception {
		return Integer.parseInt(next());
	}

	static long nextLong() throws Exception {
		return Long.parseLong(next());
	}

	static class Solver {
	    int k;
	    int get(ArrayList<Integer> r, ArrayList<Integer> o) {
	        int pr = 0, po = 0;
            int sum = 0;
            boolean fl = false;
            if (r.size() != 0 && o.size() != 0) {
                if (r.size() + o.size() >= k) {
                    fl = true;
                    for (int i = 0; i < k - 1; i++) {
                        if (pr != r.size() && po != o.size()) {
                            if (r.get(pr) < o.get(po)) {
                                sum += o.get(po++);
                            }
                            else {
                                sum += r.get(pr++);
                            }
                        }
                        else if (pr != r.size()) {
                            sum += r.get(pr++);
                        }
                        else {
                            sum += o.get(po++);
                        }
                    }
                }
                if (pr == 0) {
                    sum += r.get(0);
                }
                else if (po == 0) {
                    sum += o.get(0);
                }
                else {
                    if (pr != r.size() && po != o.size()) {
                        if (r.get(pr) < o.get(po)) {
                            sum += o.get(po++);
                        }
                        else {
                            sum += r.get(pr++);
                        }
                    }
                    else if (pr != r.size()) {
                        sum += r.get(pr++);
                    }
                    else {
                        sum += o.get(po++);
                    }
                }
            }
            return sum;
	    }

		public void solve() throws Exception {
			int n = nextInt();
			k = nextInt();
			ArrayList<Integer> r = new ArrayList<Integer>();
			ArrayList<Integer> o = new ArrayList<Integer>();
			ArrayList<Integer> w = new ArrayList<Integer>();
			int[] t = new int[n];
			for (int i = 0; i < n; i++) {
			    t[i] = nextInt();
			}
			String s = next();
			for (int i = 0; i < n; i++) {
			    if (s.charAt(i) == 'R') {
			        r.add(t[i]);
			    }
			    else if (s.charAt(i) == 'O') {
			        o.add(t[i]);
			    }
			    else {
			        w.add(t[i]);
			    }
			}
			Collections.sort(r, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    return -(o1.toString().compareTo(o2.toString()));
                }
            });
            Collections.sort(o, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    return -(o1.toString().compareTo(o2.toString()));
                }
            });
            Collections.sort(w, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    return -(o1.toString().compareTo(o2.toString()));
                }
            });
            int ans = Math.max(get(r, o), get(w, o));
            if (ans == 0) {
                out.println(-1);
            }
            else {
                out.println(ans);
            }
            out.flush();
		}
	}

	public static void main(String[] args) throws Exception {
		(new Solver()).solve();
	}
}

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

//        Scanner sc = new Scanner(new File("src/input.in"));
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int k = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }

        ArrayList<Integer> red = new ArrayList<>();
        ArrayList<Integer> orange = new ArrayList<>();
        ArrayList<Integer> white = new ArrayList<>();

        String s = sc.nextLine();
        s = sc.nextLine();
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            int x = list.get(i);
            if (c == 'R') {
                red.add(x);
            } else if (c == 'O') {
                orange.add(x);
            } else {
                white.add(x);
            }
        }

        if (k <= 1) {
            System.out.println(-1);
            return;
        }
        Collections.sort(orange);
        Collections.sort(white);
        Collections.sort(red);

        if (orange.size() == 0) {
            System.out.println(-1);
            return;
        }

        int mo;
        int mw;
        int mr;

        if (red.size() == 0) {
            mr = Integer.MIN_VALUE;
        } else {
            mr = red.get(red.size() - 1);
            red.remove(red.size() - 1);
        }

        if (white.size() == 0) {
            mw = Integer.MIN_VALUE;
        } else {
            mw = white.get(white.size() - 1);
            white.remove(white.size() - 1);
        }

        mo = orange.get(orange.size() - 1);

        orange.remove(orange.size() - 1);

        ArrayList<Integer> or = new ArrayList<>(orange);
        ArrayList<Integer> ow = new ArrayList<>(orange);

        or.addAll(red);
        Collections.sort(or);

        ow.addAll(white);
        Collections.sort(ow);

        int sr = mo + mr;
        for (int i = 0; i < k - 2 && i < or.size(); i++) {
            sr += or.get(or.size() - i - 1);
        }

        int sw = mo + mw;
        for (int i = 0; i < k - 2 && i < ow.size(); i++) {
            sw += ow.get(ow.size() - i - 1);
        }

        if (or.size() < k - 2 && ow.size() < k - 2) {
            System.out.println(-1);
            return;
        }

        if (or.size() < k - 2) {
            System.out.println(sw);
            return;
        }

        if (ow.size() < k - 2) {
            System.out.println(sr);
            return;
        }
        System.out.println(Math.max(sr, sw));
    }

}

import java.util.*;
import java.io.*;

public class Main{
    private final static String[] local = {"5 3\n" +
            "4 3 4 1 6\n" +
            "RROWW","5 2\n" +
            "10 20 14 20 11\n" +
            "RRRRR","11 5\n" +
            "5 6 3 2 3 4 7 5 4 5 6\n" +
            "RWOORWORROW","",""};

    static class Task {

        private void solve() {
            int n = ni(), k = ni();
            int[] b = na(n);
            char[] input = ns().toCharArray();
            int cnt1 = 0, cnt2 = 0, cnt3 = 0;
            ArrayList<Integer> list1 = new ArrayList<>();
            ArrayList<Integer> list3 = new ArrayList<>();
            int max0 = 0, maxr = 0, maxw = 0;
            for (int i = 0; i < n; ++i) {
                char c = input[i];
                if (c == 'R') {
                    cnt1++;
                    list1.add(b[i]);
                    maxr = Math.max(maxr, b[i]);
                }
                else if (c == 'O') {
                    cnt2++;
                    max0 = Math.max(b[i], max0);
                }
                else {
                    cnt3++;
                    list3.add(b[i]);
                    maxw = Math.max(maxw, b[i]);
                }
            }
            if (cnt2 == 0 || ((cnt1 + cnt2 < k) && (cnt2 + cnt3) < k) || (cnt1 == 0 && cnt3 == 0)) {
                out.println(-1);
                return;
            }
            for (int i = 0; i < list1.size(); ++i) {
                if (list1.get(i) == maxr) {
                    list1.remove(i);
                    break;
                }
            }
            for (int i = 0; i < list3.size(); ++i) {
                if (list3.get(i) == maxw) {
                    list3.remove(i);
                    break;
                }
            }
            debug(list1);
            debug(list3);
            boolean flag = false;
            for (int i = 0; i < n; ++i) {
                if (input[i] != 'O') continue;
                if (b[i] == max0 && !flag) {
                    flag = true;
                } else {
                    list1.add(b[i]);
                    list3.add(b[i]);
                }
            }
            debug(maxr);
            debug(maxw);
            debug(list1);
            debug(list3);
            if (cnt1 + cnt2 >= k && cnt2 + cnt3 >= k) {
                out.println(Math.max(get(list1, max0 + maxr, k - 2), get(list3, max0 + maxw, k - 2)));
            } else if (cnt1 + cnt2 >= k) {
                out.println(get(list1, max0 + maxr, k - 2));
            } else {
                out.println(get(list3, max0 + maxw, k - 2));
            }
        }

        int get(ArrayList<Integer> list, int init, int k) {
            int ans = init;
            Collections.sort(list);
            Collections.reverse(list);
            Iterator<Integer> it1 = list.iterator();
            for (int i = 0; i < k; ++i)
                ans += it1.next();
            return ans;
        }

        void run(String INPUT, boolean oj){this.oj=oj;out=new PrintWriter(System.out);inputStream = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());long startTime=System.currentTimeMillis();solve();out.flush();long endTime=System.currentTimeMillis();debug(endTime - startTime + "ms");}
        private InputStream inputStream;private PrintWriter out;private boolean oj;private byte[] inbuf=new byte[1024];private int lenbuf=0,ptrbuf=0;
        private int readByte(){if(lenbuf == -1)throw new RuntimeException();if(ptrbuf>=lenbuf){ptrbuf=0;try{lenbuf=inputStream.read(inbuf);}catch(IOException e){throw new RuntimeException();}if(lenbuf<=0) return -1;}return inbuf[ptrbuf++]; }
        private boolean isSpaceChar(int c){return !(c>=33&&c<=126);}private int skip(){int b;while((b=readByte())!=-1&&isSpaceChar(b));return b;}
        private int[] na(int n){int[] a = new int[n]; for(int i = 0; i < n; ++i) a[i] = ni(); return a;}
        private double nd(){return Double.parseDouble(ns());}private char nc(){return (char)skip();}private int ni(){return Integer.parseInt(ns());}private long nl(){return Long.parseLong(ns());}
        private String ns(){int b = skip();StringBuilder sb = new StringBuilder();while (!(isSpaceChar(b))) {sb.appendCodePoint(b);b = readByte();}return sb.toString(); }
        private int debugCnt = 0;private void debug(Object... o){if(!oj) System.out.println("debug:"+ ++debugCnt + " " + Arrays.deepToString(o));}
    }
    public static void main(String[] args){
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        //boolean oj = true;
        if(oj)new Task().run(null, true);else{int test = 1;for(String aLocal : local){if(aLocal.equals(""))continue;System.out.println("Test" + test++ + ":");new Task().run(aLocal,false);}}
    }
}

import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
    private final static String[] local = {"5 3\n" +
            "4 3 4 1 6\n" +
            "RROWW","5 2\n" +
            "10 20 14 20 11\n" +
            "RRRRR","11 5\n" +
            "5 6 3 2 3 4 7 5 4 5 6\n" +
            "RWOORWORROW","",""};

    static class Task {

        private void solve() {
            int n = ni(), k = ni();
            int[] b = na(n);
            char[] input = ns().toCharArray();
            int cnt1 = 0, cnt2 = 0, cnt3 = 0;
            ArrayList<Integer> list1 = new ArrayList<>();
            ArrayList<Integer> list3 = new ArrayList<>();
            int max0 = 0;
            for (int i = 0; i < n; ++i) {
                char c = input[i];
                if (c == 'R') {
                    cnt1++;
                    list1.add(b[i]);
                }
                else if (c == 'O') {
                    cnt2++;
                    max0 = Math.max(b[i], max0);
                }
                else {
                    cnt3++;
                    list3.add(b[i]);
                }
            }
            if (cnt2 == 0 || ((cnt1 + cnt2 < k) && (cnt2 + cnt3) < k) || (cnt1 == 0 && cnt3 == 0)) {
                out.println(-1);
                return;
            }
            boolean flag = false;
            for (int i = 0; i < n; ++i) {
                if (input[i] != 'O') continue;
                if (b[i] == max0 && !flag) {
                    flag = true;
                } else {
                    list1.add(b[i]);
                    list3.add(b[i]);
                }
            }
            if (cnt1 + cnt2 >= k && cnt2 + cnt3 >= k) {
                int ans1 = get(list1, max0, k - 1);
                int ans2 = get(list3, max0, k - 1);
                out.println(Math.max(ans1, ans2));
            } else if (cnt1 + cnt2 >= k) {
                out.println(get(list1, max0, k - 1));
            } else {
                out.println(get(list3, max0, k - 1));
            }
        }

        int get(ArrayList<Integer> list, int init, int k) {
            int ans = init;
            Collections.sort(list);
            Collections.reverse(list);
            Iterator<Integer> it1 = list.iterator();
            for (int i = 0; i < k; ++i)
                ans += it1.next();
            return ans;
        }

        void run(String INPUT, boolean oj){this.oj=oj;out=new PrintWriter(System.out);inputStream = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());long startTime=System.currentTimeMillis();solve();out.flush();long endTime=System.currentTimeMillis();debug(endTime - startTime + "ms");}
        private InputStream inputStream;private PrintWriter out;private boolean oj;private byte[] inbuf=new byte[1024];private int lenbuf=0,ptrbuf=0;
        private int readByte(){if(lenbuf == -1)throw new RuntimeException();if(ptrbuf>=lenbuf){ptrbuf=0;try{lenbuf=inputStream.read(inbuf);}catch(IOException e){throw new RuntimeException();}if(lenbuf<=0) return -1;}return inbuf[ptrbuf++]; }
        private boolean isSpaceChar(int c){return !(c>=33&&c<=126);}private int skip(){int b;while((b=readByte())!=-1&&isSpaceChar(b));return b;}
        private int[] na(int n){int[] a = new int[n]; for(int i = 0; i < n; ++i) a[i] = ni(); return a;}
        private double nd(){return Double.parseDouble(ns());}private char nc(){return (char)skip();}private int ni(){return Integer.parseInt(ns());}private long nl(){return Long.parseLong(ns());}
        private String ns(){int b = skip();StringBuilder sb = new StringBuilder();while (!(isSpaceChar(b))) {sb.appendCodePoint(b);b = readByte();}return sb.toString(); }
        private int debugCnt = 0;private void debug(Object... o){if(!oj) System.out.println("debug:"+ ++debugCnt + " " + Arrays.deepToString(o));}
    }
    public static void main(String[] args){
        boolean oj = System.getProperty("ONLINE_JUDGE") != null;
        //boolean oj = true;
        if(oj)new Task().run(null, true);else{int test = 1;for(String aLocal : local){if(aLocal.equals(""))continue;System.out.println("Test" + test++ + ":");new Task().run(aLocal,false);}}
    }
}