# Untitled

unknown
plain_text
20 days ago
1.9 kB
1
Indexable
Never
```import java.util.Scanner;

public class Main {
static int companies, mines, ANS;

static int MIN(int x, int y) {
return (x >= y) ? y : x;
}

static int MAX(int x, int y) {
return (x >= y) ? x : y;
}

static void calculateOilMines(int i, int[] oilMines, boolean[] visited, int minV, int maxV, int sum,
int nodes, int companies, int mines) {
// Base Case
if (visited[i]) {
int newMin = MIN(sum, minV);
int newMax = MAX(sum, maxV);

if (nodes == companies - 1) {
ANS = Math.min(ANS, newMax - newMin);
}
return;
}

// Main Case
visited[i] = true;
int j = (i + 1) % mines;

calculateOilMines(j, oilMines, visited, minV, maxV, sum + oilMines[i], nodes, companies, mines);

int newMin = MIN(sum, minV);
int newMax = MAX(sum, maxV);

calculateOilMines(j, oilMines, visited, newMin, newMax, oilMines[i], nodes + 1, companies, mines);

visited[i] = false;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
companies = sc.nextInt();
mines = sc.nextInt();
int[] oilMines = new int[mines];
boolean[] visited = new boolean[mines];

for (int i = 0; i < mines; i++) {
oilMines[i] = sc.nextInt();
visited[i] = false;
}

ANS = Integer.MAX_VALUE;
for (int i = 0; i < mines; i++) {
calculateOilMines(i, oilMines, visited, Integer.MAX_VALUE, Integer.MIN_VALUE, 0, 0, companies, mines);
}

System.out.println(ANS);
}
sc.close();
}
}
```