Codeforces Round #377 (Div. 2) Exam Solution
/**
*
* @author svatantra
*/
import java.io.*;
import java.util.*;
public class Four implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
try {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
} catch (Exception e) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine(), " :");
} catch (Exception e) {
return null;
}
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException {
int[] res = new int[size];
for (int i = 0; i < size; i++) {
res[i] = readInt();
}
return res;
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
public static void main(String[] args) {
new Thread(null, new Four(), "", 1l * 200 * 1024 * 1024).start();
}
long timeBegin, timeEnd;
void time() {
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
long memoryTotal, memoryFree;
void memory() {
memoryFree = Runtime.getRuntime().freeMemory();
System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
+ " KB");
}
public void run() {
try {
timeBegin = System.currentTimeMillis();
memoryTotal = Runtime.getRuntime().freeMemory();
init();
solve();
out.close();
if (System.getProperty("ONLINE_JUDGE") == null) {
time();
memory();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
int m;
int num[];
int st[];
boolean done[];
int count[];
void solve() throws IOException
{
int n=readInt();
m=readInt();
done=new boolean[m+1];
count=new int [m+1];
num=readIntArray(n);
st=new int[m+1];
int i;
for(i=1;i<=m;i++)
st[i]=readInt();
int min=0;
int max=n-1;
int prev=-1;
int mid=0;
boolean midf=false;
boolean prevf=false;
while(min<=max)
{
mid=(min+max)>>1;
if(can(mid)==true)
{
// System.out.print("True for "+mid);
max=mid-1;
prev=mid;
prevf=true;
}
else
{
midf=false;
min=mid+1;
}
}
if(can(mid))
{
out.println(mid+1);
}
else if(prevf)
{
out.println(prev+1);
}
else
{
out.print("-1");
}
}
boolean can(int mid)
{
// System.out.println("Mid="+mid);
// System.out.println("m="+m);
//System.out.println("Count="+count.length);
Arrays.fill(done,false);
Arrays.fill(count,0);
done[0]=true;
int i;
for(i=0;i<=mid;i++){
//System.out.println("Val="+num[i]);
count[num[i]]++;
}
int have=count[0];
int used=0;
for(i=mid;i>=0;i--)
{
if(!done[num[i]])
{
have-=st[num[i]];
count[num[i]]--;
have+=count[num[i]];
done[num[i]]=true;
used+=st[num[i]];
// System.out.println("used="+used);
}
else
{
if(used>0)
{
used--;
}
else
have--;
}
}
if(have>=0)
{
for(i=1;i<=m;i++)
{
if(done[i]==false)
{
return false;
}
}
return true;
}
return false;
}
}
Comments
Post a Comment