Submission #3836041
Source Code Expand
import static java.lang.Math.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
MyInput in = new MyInput(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskX solver = new TaskX();
solver.solve(1, in, out);
out.close();
}
static int INF = 1 << 30;
static long LINF = 1L << 55;
static int MOD = 1000000007;
static int[] mh4 = { 0, -1, 1, 0 };
static int[] mw4 = { -1, 0, 0, 1 };
static int[] mh8 = { -1, -1, -1, 0, 0, 1, 1, 1 };
static int[] mw8 = { -1, 0, 1, -1, 1, -1, 0, 1 };
static class TaskX {
public void solve(int testNumber, MyInput in, PrintWriter out) {
int n = in.nextInt(), m = in.nextInt();
int[] a = in.nextIntArray(n);
P[] ps = new P[m];
for (int i = 0; i < m; i++) {
int l = in.nextInt()-1, r = in.nextInt()-1;
ps[i] = new P(l, r);
}
Arrays.sort(ps);
int[] next = new int[n];
Arrays.fill(next, -1);
int idx = 0, jmp = -1;
for (int i = 0; i < n; i++) {
while (idx < m && ps[idx].first <= i && i <= ps[idx].second) {
jmp = Math.max(jmp, ps[idx].second);
idx++;
}
next[i] = max(jmp, i);
}
long[] dp = new long[200010];
for (int i = 0; i < n; i++) {
dp[i+1] = max(dp[i+1], dp[i]);
dp[next[i]+1] = max(dp[next[i]+1], dp[i] + a[i]);
}
out.println(dp[n]);
}
}
static class P implements Comparable<P> {
int first, second;
public P(int first, int second) {
super();
this.first = first;
this.second = second;
}
@Override
public int compareTo(P o) {
int c1 = Integer.compare(this.first, o.first);
int c2 = Integer.compare(this.second, o.second);
return c1 == 0 ? c2 : c1;
}
@Override
public String toString() {
return "P [first=" + first + ", second=" + second + "]";
}
}
static class MyInput {
private final BufferedReader in;
private static int pos;
private static int readLen;
private static final char[] buffer = new char[1024 * 8];
private static char[] str = new char[500 * 8 * 2];
private static boolean[] isDigit = new boolean[256];
private static boolean[] isSpace = new boolean[256];
private static boolean[] isLineSep = new boolean[256];
static {
for (int i = 0; i < 10; i++) {
isDigit['0' + i] = true;
}
isDigit['-'] = true;
isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true;
isLineSep['\r'] = isLineSep['\n'] = true;
}
public MyInput(InputStream is) {
in = new BufferedReader(new InputStreamReader(is));
}
public int read() {
if (pos >= readLen) {
pos = 0;
try {
readLen = in.read(buffer);
} catch (IOException e) {
throw new RuntimeException();
}
if (readLen <= 0) {
throw new MyInput.EndOfFileRuntimeException();
}
}
return buffer[pos++];
}
public int nextInt() {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);
int i = 0;
int ret = 0;
if (str[0] == '-') {
i = 1;
}
for (; i < len; i++)
ret = ret * 10 + str[i] - '0';
if (str[0] == '-') {
ret = -ret;
}
return ret;
}
public long nextLong() {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);
int i = 0;
long ret = 0;
if (str[0] == '-') {
i = 1;
}
for (; i < len; i++)
ret = ret * 10 + str[i] - '0';
if (str[0] == '-') {
ret = -ret;
}
return ret;
}
public char nextChar() {
while (true) {
final int c = read();
if (!isSpace[c]) {
return (char) c;
}
}
}
public String nextString() {
return new String(nextChars());
}
public char[] nextChars() {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);
return Arrays.copyOf(str, len);
}
int reads(int len, boolean[] accept) {
try {
while (true) {
final int c = read();
if (accept[c]) {
break;
}
if (str.length == len) {
char[] rep = new char[str.length * 3 / 2];
System.arraycopy(str, 0, rep, 0, str.length);
str = rep;
}
str[len++] = (char) c;
}
} catch (MyInput.EndOfFileRuntimeException e) {
}
return len;
}
public int[] nextIntArray(final int n) {
final int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
public int[] nextIntArray1Index(final int n) {
final int[] res = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
res[i] = nextInt();
}
return res;
}
public int[] nextIntArrayDec(final int n) {
final int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt() - 1;
}
return res;
}
public long[] nextLongArray(final int n) {
final long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
public long[] nextLongArray1Index(final int n) {
final long[] res = new long[n + 1];
for (int i = 1; i < n + 1; i++) {
res[i] = nextLong();
}
return res;
}
public long[] nextLongArrayDec(final int n) {
final long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong() - 1;
}
return res;
}
public double nextDouble() {
return Double.parseDouble(nextString());
}
public double[] nextDoubleArray(int n) {
double[] res = new double[n];
for (int i = 0; i < n; i++) {
res[i] = nextDouble();
}
return res;
}
static class EndOfFileRuntimeException extends RuntimeException {
}
}
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - イルミネーション (Illumination) |
| User | tutuz |
| Language | Java8 (OpenJDK 1.8.0) |
| Score | 100 |
| Code Size | 5991 Byte |
| Status | AC |
| Exec Time | 561 ms |
| Memory | 32164 KiB |
Judge Result
| Set Name | Sample | Subtask1 | Subtask2 | Subtask3 | Subtask4 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 10 / 10 | 30 / 30 | 30 / 30 | 30 / 30 | ||||||||||
| Status |
|
|
|
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample-01.txt, sample-02.txt, sample-03.txt |
| Subtask1 | sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt |
| Subtask2 | sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt |
| Subtask3 | sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 03-01.txt, 03-02.txt, 03-03.txt, 03-04.txt, 03-05.txt, 03-06.txt, 03-07.txt, 03-08.txt, 03-09.txt, 03-10.txt, 03-11.txt, 03-12.txt, 03-13.txt |
| Subtask4 | 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 03-01.txt, 03-02.txt, 03-03.txt, 03-04.txt, 03-05.txt, 03-06.txt, 03-07.txt, 03-08.txt, 03-09.txt, 03-10.txt, 03-11.txt, 03-12.txt, 03-13.txt, 04-01.txt, 04-02.txt, 04-03.txt, 04-04.txt, 04-05.txt, 04-06.txt, 04-07.txt, 04-08.txt, 04-09.txt, 04-10.txt, 04-11.txt, sample-01.txt, sample-02.txt, sample-03.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 01-01.txt | AC | 77 ms | 22228 KiB |
| 01-02.txt | AC | 75 ms | 21844 KiB |
| 01-03.txt | AC | 77 ms | 23252 KiB |
| 01-04.txt | AC | 76 ms | 21460 KiB |
| 01-05.txt | AC | 76 ms | 20436 KiB |
| 01-06.txt | AC | 78 ms | 21204 KiB |
| 01-07.txt | AC | 75 ms | 22868 KiB |
| 01-08.txt | AC | 77 ms | 22484 KiB |
| 01-09.txt | AC | 77 ms | 23380 KiB |
| 01-10.txt | AC | 76 ms | 20308 KiB |
| 01-11.txt | AC | 75 ms | 21460 KiB |
| 01-12.txt | AC | 75 ms | 21076 KiB |
| 01-13.txt | AC | 76 ms | 18004 KiB |
| 01-14.txt | AC | 77 ms | 23252 KiB |
| 01-15.txt | AC | 76 ms | 21972 KiB |
| 01-16.txt | AC | 75 ms | 24660 KiB |
| 01-17.txt | AC | 75 ms | 21588 KiB |
| 02-01.txt | AC | 79 ms | 23508 KiB |
| 02-02.txt | AC | 80 ms | 23252 KiB |
| 02-03.txt | AC | 79 ms | 21844 KiB |
| 02-04.txt | AC | 78 ms | 21332 KiB |
| 02-05.txt | AC | 78 ms | 20564 KiB |
| 02-06.txt | AC | 75 ms | 21716 KiB |
| 02-07.txt | AC | 77 ms | 22868 KiB |
| 02-08.txt | AC | 79 ms | 23380 KiB |
| 02-09.txt | AC | 77 ms | 21204 KiB |
| 02-10.txt | AC | 78 ms | 21076 KiB |
| 02-11.txt | AC | 78 ms | 22996 KiB |
| 02-12.txt | AC | 78 ms | 21716 KiB |
| 02-13.txt | AC | 79 ms | 21460 KiB |
| 03-01.txt | AC | 103 ms | 22100 KiB |
| 03-02.txt | AC | 112 ms | 25684 KiB |
| 03-03.txt | AC | 102 ms | 26196 KiB |
| 03-04.txt | AC | 114 ms | 22740 KiB |
| 03-05.txt | AC | 105 ms | 22996 KiB |
| 03-06.txt | AC | 92 ms | 22612 KiB |
| 03-07.txt | AC | 94 ms | 23252 KiB |
| 03-08.txt | AC | 95 ms | 21460 KiB |
| 03-09.txt | AC | 94 ms | 22612 KiB |
| 03-10.txt | AC | 94 ms | 23764 KiB |
| 03-11.txt | AC | 93 ms | 23508 KiB |
| 03-12.txt | AC | 86 ms | 25556 KiB |
| 03-13.txt | AC | 94 ms | 26068 KiB |
| 04-01.txt | AC | 158 ms | 24264 KiB |
| 04-02.txt | AC | 159 ms | 24392 KiB |
| 04-03.txt | AC | 462 ms | 31524 KiB |
| 04-04.txt | AC | 538 ms | 30352 KiB |
| 04-05.txt | AC | 507 ms | 29944 KiB |
| 04-06.txt | AC | 561 ms | 32164 KiB |
| 04-07.txt | AC | 484 ms | 30764 KiB |
| 04-08.txt | AC | 534 ms | 30032 KiB |
| 04-09.txt | AC | 192 ms | 26924 KiB |
| 04-10.txt | AC | 159 ms | 23272 KiB |
| 04-11.txt | AC | 159 ms | 24276 KiB |
| sample-01.txt | AC | 76 ms | 20308 KiB |
| sample-02.txt | AC | 76 ms | 21460 KiB |
| sample-03.txt | AC | 77 ms | 21972 KiB |