# Java stack limit

I encountered severe problems with Java’s stack limit. With many silver issues, I can’t really use recursions to solve them (for example, December Silver Problem 1). What should I do?

``````import java.io.*;
import java.util.*;
public class BarnTree {
static ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>();
static long[] hays;
static long[] subsums;
static long count = 0;
static ArrayList<String> orders;
static PrintWriter pr = new PrintWriter(System.out);
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
long average = 0;
hays = new long[n+1];
subsums =new long[n+1];
for(int i  = 1;i<=n;i++) {
hays[i] = Integer.parseInt(st.nextToken());
average += hays[i];
}
average /= n;
for(int i = 1; i<=n;i++) hays[i] -= average;
for(int i = 0;i<n-1;i++) {
//System.out.println(i);
int a = Integer.parseInt(curl.nextToken());
int b = Integer.parseInt(curl.nextToken());
}
orders = new ArrayList<String>();
dfssum(1,1);
dfsprint(1,1);
pr.println(count);
for(String o:orders) pr.println(o);
pr.close();
}
static long dfssum(int cur, int last) {
long cursum = hays[cur];
subsums[cur] = cursum;
return cursum;
}
static void dfsprint(int cur,int last) {
if(subsums[next]>0&&next!=last)dfsprint(next,cur);
}
if(subsums[next]<0&&next!=last) {
StringBuilder sb =new StringBuilder();
count++;
dfsprint(next,cur);
}
}
if(subsums[cur]>0&&cur!=1) {