@@ -0,0 +1,2043 @@
+import java.util.*;%0A%0Apublic class Solution %7B%0A%0A    public static void main(String args%5B%5D) %7B%0A        Scanner in = new Scanner(System.in);%0A        int L = in.nextInt();%0A        int C = in.nextInt();%0A        int N = in.nextInt();%0A        %0A        ArrayList%3Cint%5B%5D%3E groups = new ArrayList%3Cint%5B%5D%3E();%0A        for (int i = 0; i %3C N; i++) %7B%0A            groups.add(new int%5B%5D%7Bin.nextInt(), i%7D);%0A        %7D%0A%0A        long cntCoins = 0L;%0A        ArrayList%3Clong%5B%5D%3E firsts = new ArrayList%3Clong%5B%5D%3E();%0A        int firstExists = 0;%0A        boolean firstFind = false;%0A        int currIndex = 0;%0A        for(int i=0;i%3CC;i++)%7B%0A            int cntPpl = 0;%0A            int cntRows = 0;%0A            if(!firstFind)%7B%0A%09            for(int j=0;j%3Cfirsts.size();j++)%7B%0A%09            %09if(groups.get(0)%5B0%5D == firsts.get(j)%5B0%5D && groups.get(0)%5B1%5D == firsts.get(j)%5B1%5D)%7B%0A%09            %09%09firstExists  = j;%0A%09            %09%09currIndex = j;%0A%09            %09%09firstFind = true;%0A%09            %09%09break;%0A%09            %09%7D%0A%09            %7D%0A%09            if(!firstFind)%7B%0A%09            %09firsts.add(new long%5B%5D%7Bgroups.get(0)%5B0%5D,groups.get(0)%5B1%5D, 0%7D);%0A%09%09            while(groups.get(0)%5B0%5D + cntPpl %3C= L && cntRows %3C N)%7B%0A%09%09                cntPpl += groups.get(0)%5B0%5D;%0A%09%09                int%5B%5D tempGroup1 = groups.get(0);%0A%09%09                groups.remove(0);%0A%09%09                groups.add(tempGroup1);%0A%09%09                cntRows++;%0A%09%09            %7D%0A%09%09            cntCoins += cntPpl;%0A%09%09            firsts.get(firsts.size() - 1)%5B2%5D = cntPpl;%0A%09            %7D%0A%09            else%7B%0A%09            %09cntCoins += firsts.get(firstExists)%5B2%5D;%0A%09            %09if(currIndex %3C firsts.size() - 1)%7B%0A%09            %09%09currIndex++;%0A%09            %09%7D%0A%09            %09else%7B%0A%09            %09%09currIndex = 0;%0A%09            %09%7D%0A%09            %7D%0A            %7D%0A            else%7B%0A            %09cntCoins += firsts.get(currIndex)%5B2%5D;%0A            %09if(currIndex %3C firsts.size() - 1)%7B%0A            %09%09currIndex++;%0A            %09%7D%0A            %09else%7B%0A            %09%09currIndex = firstExists;%0A            %09%7D%0A            %7D%0A        %7D%0A        System.out.println(cntCoins);%0A    %7D%0A%7D%0A

Create Solution.java