import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
// two pointers
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int k = s.nextInt();
int r = s.nextInt();
int[] freq = new int[k+5];
int[] req = new int[k+5];
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = s.nextInt();
freq[arr[i]]++;
}
for(int i = 0; i < r; i++) {
int v1 = s.nextInt();
int v2 = s.nextInt();
req[v1] += v2;
}
for(int i = 0; i < k + 5; i++) {
if(freq[i] < req[i]) {
System.out.println("impossible");
return;
}
}
int p1 = 0;
int p2 = n - 1;
while(p1 < p2) {
boolean works = false;
if(freq[arr[p1]] - 1 >= req[arr[p1]]) {
freq[arr[p1]]--;
p1++;
works = true;
}
if(freq[arr[p2]] - 1 >= req[arr[p2]]) {
freq[arr[p2]]--;
p2--;
works = true;
}
if(!works) {
System.out.println(p2 - p1 + 1);
return;
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
128 ms |
12356 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
129 ms |
11948 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
129 ms |
12028 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
165 ms |
11900 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |