Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created October 26, 2017 14:16
Show Gist options
  • Save chermehdi/f4135c704ed8b88304b640a5cc96ff74 to your computer and use it in GitHub Desktop.
Save chermehdi/f4135c704ed8b88304b640a5cc96ff74 to your computer and use it in GitHub Desktop.
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.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author MaxHeap
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
FruitBasket solver = new FruitBasket();
solver.solve(1, in, out);
out.close();
}
static class FruitBasket {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
int[] fruits = new int[n];
for (int i = 0; i < n; i++)
fruits[i] = in.nextInt();
Arrays.sort(fruits);
long sum = 0L;
long cur = 0;
for (int i = 0; i < n; i++) cur += fruits[i];
sum = (1L << (n - 1)) * cur;
int ind = Arrays.binarySearch(fruits, 200);
if (ind < 0) ind = -(ind + 1);
int mask = (1 << (ind)) - 1;
for (int i = 1; i <= mask; i++) {
long loc = 0;
for (int j = 0; j < ind; j++) {
if ((i & (1 << j)) != 0) {
loc += fruits[j];
}
}
if (loc < 200) {
sum -= loc;
}
}
out.println(sum);
}
}
static class FastReader {
BufferedReader reader;
StringTokenizer st;
public FastReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment