0 0 投票数

import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] d = new int[n];
int[] p = new int[n];
for (int i = 0; i < n; i++)
d[i] = in.nextInt();
for (int i = 0; i < n; i++)
p[i] = in.nextInt();
PriorityQueue<Q> pq = new PriorityQueue<>();
int i = 0;
while (i < n) {
Q q = pq.peek();
if (d[i] > q.power) {
pq.poll();
pq.add(new Q(q.cost + p[i], q.power + d[i]));
} else {
pq.add(new Q(q.cost + p[i], q.power + d[i]));
}
i++;
}
System.out.println(pq.peek().cost);
in.close();
}

}

class Q implements Comparable<Q> {

int cost;
int power;

public Q(int cost, int power) {
super();
this.cost = cost;
this.power = power;
}

@Override
public int compareTo(Q o) {
return Integer.compare(cost, o.cost);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + cost;
result = prime * result + power;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Q other = (Q) obj;
if (cost != other.cost)
return false;
if (power != other.power)
return false;
return true;
}

}

#include <bits/stdc++.h>

using namespace std;

int main()
{
int n, m;
cin>>m;
cin>>n;
int *a = new int[n];
for (int i = 0; i < n; ++i) {
cin>>a[i];
}
sort(a, a + n);
if (a[0] != 1) {
cout<<-1<<endl;
}
int sum = 1;
int cnt = 1;
while (sum < m) {
int i = 0;
for (; i < n; ++i) {
if (a[i] > sum + 1) {
break;
}
}
sum += a[i - 1];
++cnt;
}
cout<<cnt<<endl;
}
0 0 投票数