結果

問題 No.286 Modulo Discount Store
ユーザー kitakitalilykitakitalily
提出日時 2020-01-03 14:58:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 8,942 bytes
コンパイル時間 2,303 ms
コンパイル使用メモリ 78,912 KB
実行使用メモリ 44,596 KB
最終ジャッジ日時 2024-11-22 19:10:30
合計ジャッジ時間 6,293 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
43,064 KB
testcase_01 AC 57 ms
42,888 KB
testcase_02 AC 74 ms
44,140 KB
testcase_03 AC 57 ms
43,276 KB
testcase_04 AC 55 ms
42,492 KB
testcase_05 AC 55 ms
43,148 KB
testcase_06 AC 85 ms
44,468 KB
testcase_07 AC 57 ms
43,316 KB
testcase_08 AC 56 ms
42,900 KB
testcase_09 AC 55 ms
42,500 KB
testcase_10 AC 68 ms
42,928 KB
testcase_11 AC 55 ms
42,888 KB
testcase_12 AC 56 ms
43,188 KB
testcase_13 AC 82 ms
44,212 KB
testcase_14 AC 55 ms
42,684 KB
testcase_15 AC 56 ms
42,512 KB
testcase_16 AC 55 ms
43,176 KB
testcase_17 AC 97 ms
44,596 KB
testcase_18 AC 65 ms
43,264 KB
testcase_19 AC 55 ms
43,200 KB
testcase_20 AC 56 ms
42,888 KB
testcase_21 AC 55 ms
43,064 KB
testcase_22 AC 56 ms
42,752 KB
testcase_23 AC 83 ms
43,956 KB
testcase_24 AC 54 ms
43,016 KB
testcase_25 AC 57 ms
42,888 KB
testcase_26 AC 53 ms
42,712 KB
testcase_27 AC 58 ms
43,056 KB
testcase_28 AC 90 ms
44,200 KB
testcase_29 AC 56 ms
42,888 KB
testcase_30 AC 54 ms
43,108 KB
testcase_31 AC 56 ms
43,056 KB
testcase_32 AC 57 ms
42,624 KB
testcase_33 AC 56 ms
43,116 KB
testcase_34 AC 63 ms
43,004 KB
testcase_35 AC 54 ms
42,812 KB
testcase_36 AC 54 ms
42,760 KB
testcase_37 AC 60 ms
43,040 KB
testcase_38 AC 61 ms
42,840 KB
testcase_39 AC 86 ms
44,060 KB
testcase_40 AC 55 ms
43,056 KB
testcase_41 AC 55 ms
42,624 KB
testcase_42 AC 56 ms
42,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
	static int size = 200000;
	static long[] fac = new long[size];
	static long[] finv = new long[size];
	static long[] inv = new long[size];
	static int mod = 1000000007;
	static int INF = Integer.MAX_VALUE;
	static int[] dx = {-2,-2,1,-1};
	static int[] dy = {1,-1,-2,-2};
 	public static void main(String[] args){
		FastScanner scanner = new FastScanner();
		int n = scanner.nextInt();
		int[] a = new int[n];
		for(int i = 0; i < n; i++){
			a[i] = scanner.nextInt();
		}
		int[] dp = new int[1<<n];
		Arrays.fill(dp,INF);
		dp[0] = 0;
		for(int mask = 0; mask < (1<<n); mask++){
			for(int i = 0; i < n; i++){
				if(((mask>>i)&1) == 0){
					int x = 0;
					for(int j = 0; j < n; j++){
						if(((mask>>j)&1) == 1){
							x += a[j];
						}
					}
					x %= 1000;
					if(a[i]-x < 0){
						dp[mask+(1<<i)] = Math.min(dp[mask+(1<<i)],dp[mask]);
					}else{
						dp[mask+(1<<i)] = Math.min(dp[mask+(1<<i)],dp[mask]+a[i]-x); 						
					}
				}
			}
		}
		System.out.println(dp[(1<<n)-1]);
 	}
	public static boolean isPrime(int n){
		if(n == 1) return false;
		if(n == 2 || n == 3) return true;
		for(int i = 2; i <= Math.sqrt(n); i++){
			if(n % i == 0) return false;
		}
		return true;
	}
	//n,mの最大公約数
	public static long gcd(long n, long m){
		if(m > n) return gcd(m,n);
		if(m == 0) return n;
		return gcd(m, n%m);
	}
	static class Edge{
		int to;
		Edge(int to){
			this.to = to;
		}
	}
	static class Edge2 implements Comparable<Edge2>{
		int from;
		int to;
		int id;
		Edge2(int from, int to, int id){
			this.from = from;
			this.to = to;
			this.id = id;
		}
		public int compareTo(Edge2 e){
			return id-e.id;
		}
	}
	  static class UnionFind {
	    int[] parent;
	    public UnionFind(int size) {
	      parent = new int[size];
	      Arrays.fill(parent, -1);
	    }
	    public boolean unite(int x, int y) {
	      x = root(x);
	      y = root(y);
	      if (x != y) {
	        if (parent[y] < parent[x]) {
	          int tmp = y;
	          y = x;
	          x = tmp;
	        }
	        parent[x] += parent[y];
	        parent[y] = x;
	        return true;
	      }
	      return false;
	    }
	    public boolean same(int x, int y) {
	      return root(x) == root(y);
	    }
	    public int root(int x) {
	      return parent[x] < 0 ? x : (parent[x] = root(parent[x]));
	    }
	    public int size(int x) {
	      return -parent[root(x)];
	    }
	  }
	static class BIT{
		int n;
		int[] bit;
		public BIT(int n){
			this.n = n;
			bit = new int[n+1];
		}
		void add(int idx, int val){
			for(int i = idx+1; i <= n; i += i&(-i)) bit[i-1] += val;
		}
		int sum(int idx){
			int res = 0;
			for(int i = idx+1; i > 0; i -= i&(-i)) res += bit[i-1];
			return res;
		}
		int sum(int begin, int end){
			if(begin == 0) return sum(end);
			return sum(end)-sum(begin-1);
		}
	}
	static class RMQ {
		private int size_, dat[];
		private int query_(int a, int b, int k, int l, int r) {
			if(r <= a || b <= l) return 2147483647;
			if(a <= l && r <= b) return dat[k];
			int lc = query_(a, b, 2 * k, l, (l + r) / 2);
			int rc = query_(a, b, 2 * k + 1, (l + r) / 2, r);
			return Math.min(lc, rc);
		}
		RMQ(int s) {
			for(size_ = 1; size_ < s;) size_ *= 2;
			dat = new int[size_ * 2];
			for(int i = 0; i < size_ * 2; i++) dat[i] = 2147483647;
		}
		public void update(int pos, int x) {
			pos += size_; dat[pos] = x;
			while(pos > 1) {
				pos /= 2;
				dat[pos] = Math.min(dat[2 * pos], dat[2 * pos + 1]);
			}
		}
		public int query(int l, int r) {
			return query_(l, r, 1, 0, size_);
		}
	}
	public static int upperBound(long[] array, long value) {
			 int low = 0;
			 int high = array.length;
			 int mid;
			 while( low < high ) {
					 mid = ((high - low) >>> 1) + low; // (high + low) / 2
					 if( array[mid] <= value ) {
							 low = mid + 1;
					 } else {
							 high = mid;
					 }
			 }
			 return low;
	 }
	 public static final int lowerBound(final long[] arr, final long value) {
			int low = 0;
			int high = arr.length;
			int mid;
			while (low < high){
					mid = ((high - low) >>> 1) + low;    //(low + high) / 2 (オーバーフロー対策)
					if (arr[mid] < value) {
						low = mid + 1;
					} else {
						high = mid;
					}
			}
			return low;
		}
	//繰り返し二乗法
	public static long pow(long x, long n){
		long ans = 1;
		while(n > 0){
			if((n & 1) == 1){
				ans = ans * x;
				ans %= mod;
			}
			x = x * x % mod;
			n >>= 1;
		}
		return ans;
	}
	public static long div(long x, long y){
		return (x*pow(y, mod-2))%mod;
	}
	//fac, inv, finvテーブルの初期化、これ使う場合はinitComb()で初期化必要
	public static  void initComb(){
		fac[0] = finv[0] = inv[0] = fac[1] = finv[1] = inv[1] = 1;
		for (int i = 2; i < size; ++i) {
			fac[i] = fac[i - 1] * i % mod;
			inv[i] = mod - (mod / i) * inv[(int) (mod % i)] % mod;
			finv[i] = finv[i - 1] * inv[i] % mod;
		}
	}
	//nCk % mod
	public static long comb(int n, int k){
		return fac[n] * finv[k] % mod * finv[n - k] % mod;
	}
	//n! % mod
	public static long fact(int n){
		return fac[n];
	}
	//(n!)^-1 with % mod
	public static long finv(int n){
		return finv[n];
	}

	static class Pair implements Comparable<Pair>{
    int first, second;
    Pair(int a, int b){
        first = a;
        second = b;
    }
    @Override
    public boolean equals(Object o){
        if (this == o) return true;
        if (!(o instanceof Pair)) return false;
        Pair p = (Pair) o;
        return first == p.first && second == p.second;
    }
    @Override
    public int compareTo(Pair p){
        //return first == p.first ? second - p.second : first - p.first; //firstで昇順にソート
        //return (first == p.first ? second - p.second : first - p.first) * -1; //firstで降順にソート
        return second == p.second ? first - p.first : second - p.second;//secondで昇順にソート
        //return (second == p.second ? first - p.first : second - p.second)*-1;//secondで降順にソート
		    //return first * 1.0 / second > p.first * 1.0 / p.second ? 1 : -1; // first/secondの昇順にソート
		    //return first * 1.0 / second < p.first * 1.0 / p.second ? 1 : -1; // first/secondの降順にソート
				//return second * 1.0 / first > p.second * 1.0 / p.first ? 1 : -1; // second/firstの昇順にソート
		    //return second * 1.0 / first < p.second * 1.0 / p.first ? 1 : -1; // second/firstの降順にソート
				//return Math.atan2(second, first) > Math.atan2(p.second, p.first) ? 1 : -1; // second/firstの昇順にソート
				//return first + second > p.first + p.second ? 1 : -1; //first+secondの昇順にソート
				//return first + second < p.first + p.second ? 1 : -1; //first+secondの降順にソート
				//return first - second < p.first - p.second ? 1 : -1; //first-secondの降順にソート
				//return second - first < p.second - p.first ? 1 : -1; //first-secondの昇順にソート
				//return second - first < p.second - p.first ? -1 : 1; //second-firstの昇順にソート
				//return Math.atan2(second,first) > Math.atan2(p.second, p.first) ? 1 : -1;
		}
  }

	private static class FastScanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;
		private boolean hasNextByte() {
				if (ptr < buflen) {
						return true;
				}else{
						ptr = 0;
						try {
								buflen = in.read(buffer);
						} catch (IOException e) {
								e.printStackTrace();
						}
						if (buflen <= 0) {
								return false;
						}
				}
				return true;
		}
		private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
		private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
		public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
		public String next() {
				if (!hasNext()) throw new NoSuchElementException();
				StringBuilder sb = new StringBuilder();
				int b = readByte();
				while(isPrintableChar(b)) {
						sb.appendCodePoint(b);
						b = readByte();
				}
				return sb.toString();
		}
		public long nextLong() {
				if (!hasNext()) throw new NoSuchElementException();
				long n = 0;
				boolean minus = false;
				int b = readByte();
				if (b == '-') {
						minus = true;
						b = readByte();
				}
				if (b < '0' || '9' < b) {
						throw new NumberFormatException();
				}
				while(true){
						if ('0' <= b && b <= '9') {
								n *= 10;
								n += b - '0';
						}else if(b == -1 || !isPrintableChar(b)){
								return minus ? -n : n;
						}else{
								throw new NumberFormatException();
						}
						b = readByte();
				}
		}
		public int nextInt() {
				long nl = nextLong();
				if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
				return (int) nl;
		}
		public double nextDouble() { return Double.parseDouble(next());}
	}
}
0