Program that should output nth arrangement of paintings I'm currently practicing for the British informatics olympiad by doing past papers. 

I'm on question 3 of the 2015 paper and trying to use recursion to solve it. I'm pretty new to using recursion but I'm not sure what I can do to make my recursive function any more efficient. 

The program runs fine for the smaller test numbers but for larger ones my program takes upwards of 5 seconds, when it should take 1 second max. 

I don't want you to fix my code for me (this is a past question so doing that is pointless), what I want to know is: where I am going wrong with my recursive function and how I can use recursion or similar problem solving techniques in the future more efficiently.

Any help would be much appreciated as I'm going to be doing the test tomorrow!

import java.util.*;

public class Q3_2015 {

    static ArrayList&lt;String&gt; works = new ArrayList&lt;String&gt;();
    //static ArrayList&lt;String&gt; allWorks = new ArrayList&lt;String&gt;(); //used for question 2
    static int counter = 0;
    static String currentFinishedWork;
    static int a;
    static int b;
    static int c;
    static int d;
    static int n;
    static boolean fullwork;

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        a = scan.nextInt();
        b = scan.nextInt();
        c = scan.nextInt();
        d = scan.nextInt();
        n = scan.nextInt();

        for (int aAmount = 0; aAmount &lt; a; aAmount++) {

            works.add("A");
        }

        for (int bAmount = 0; bAmount &lt; b; bAmount++) {

            works.add("B");
        }

        for (int cAmount = 0; cAmount &lt; c; cAmount++) {

            works.add("C");
        }

        for (int dAmount = 0; dAmount &lt; d; dAmount++) {

            works.add("D");
        }



        //System.out.println(works);

        WorksSolver("");

        //System.out.println(allWorks); //used for question 2
        System.out.println(currentFinishedWork);



    }

    public static void WorksSolver (String currentWork) {//, int aWorksLeft, int bWorksLeft, int cWorksLeft, int dWorksLeft) {

        if (counter == n) {//allWorks.size() == n) { //used for question 2

            //System.out.println("hhee");
            return;
        }

        fullwork = true;

        if (currentWork.length() - currentWork.replaceAll("A", "").length() &lt; a) {

            fullwork = false;
            WorksSolver(currentWork + "A");
        }

        if (currentWork.length() - currentWork.replaceAll("B", "").length() &lt; b) {

            fullwork = false;
            WorksSolver(currentWork + "B");
        }

        if (currentWork.length() - currentWork.replaceAll("C", "").length() &lt; c) {

            fullwork = false;
            WorksSolver(currentWork + "C");
        }

        if (currentWork.length() - currentWork.replaceAll("D", "").length() &lt; d) {

            fullwork = false;
            WorksSolver(currentWork + "D");
        }

        if (fullwork) {

            currentFinishedWork = currentWork;
            counter ++;
            //allWorks.add(currentWork); //used for question 2
        }


        //if (aWorksLeft == 0 &amp;&amp; bWorksLeft == 0 &amp;&amp; cWorksLeft == 0 &amp;&amp; dWorksLeft == 0) {
    //}


    }
}
I have thought of the following algorithm that is NOT recursive at all and thus has linear \$o(n)\$ complexity (n being the total number of works meaning a+b+c+d+e...):

The order of arrangements in the list is alphabetical.  This means that the list will forever begin with arrangements beginning with A and then arrangements beginning with B and so on. So looking at the example list in the question, the first column of letters shows consecutive ranges of letters: AAABBBBBBCCC. You can calculate this "list of first letters" based on the input values of how many artists and how many works for each artist (I will let you figure out the exact formula, its pretty easy). So now you can determine the first letter of the nth arrangement directly from this formula.  

Once you have that, it's pretty easy to calculate the rest of the letters by removing the first letter and applying the formula as if the input was one work less.