🏆 Luyện bài tập & THI THỬ MIỄN PHÍ HÀNG TUẦN tại oj.vietnamson.com — HSG Tin 9 · Tuyển sinh 10 chuyên Tin · HSG THPT · Quốc gia · Tin học trẻ · chấm tự động, có bảng xếp hạng & lời giải → Vào thi ngay! 🏆 THI THỬ TIN HỌC MIỄN PHÍ MỖI TUẦN tại oj.vietnamson.com → Tuyển sinh 🎓 Mình nhận đào tạo lập trình giải thuật thi đấu: thi vào lớp 10 chuyên tin · thi học sinh giỏi tin học cấp tỉnh/thành · thi vào đội tuyển quốc gia · Olympic tin học sinh viên / ICPC · phỏng vấn thuật toán. Bấm vào đây để xem thông tin khóa học → Xem khóa học

VIETNAMSON OJ · THI THỬ HÀNG TUẦN

Tuần này có kỳ thi thử

Thi như thật, chấm tự động, có lời giải chi tiết ngay sau giờ thi. Miễn phí cho mọi người. Đăng ký trước, rủ bạn bè cùng vào thử sức nhé!

Các đạo hữu, cùng nhập trận nào!
Chủ đề mới: Tu TiênMỗi đề là một chặng tu luyện: trận pháp, đan dược, bí cảnh, thiên thê…
Độ khó đã được giảmTheo góp ý của các bạn, đề vừa sức hơn so với 3 tuần trước
Tính điểm và xếp hạngKỳ thi nào cũng có bảng xếp hạng: biết ngay mình ở cảnh giới nào
Đăng ký trước từ bây giờ: đến giờ chỉ việc vào làm bài!Chưa biết cách tham gia? Xem hướng dẫn

ICPC Miền Trung 2023-2024 - Bài H: Health check

Nguồn: The 2023 ICPC Vietnam Central Provincial Programming Contest (ICPC Việt Nam vòng miền Trung 2023) – Problem HĐộ khó: Trung bình

Đề bài

H. Health check
Standard I/O | Time Limit: 1s | Memory Limit: 512 MB
(Python on oj.vietnamson.com: 3s)

Chi is a busy person. But because of working too much, he is suffered from some diseases that needed to be examined and the doctor required many tests to be able to judge Chi's health.

After receiving the request, Chi looked at the calendar and saw that he had $n$, $(1 < n < 10^5)$ upcoming free days and Chi listed them on paper $a_1, a_2, \ldots, a_n$, $(1 < a_i < 10^9)$ is the distance from today to the next free day. For example, tomorrow is free, then $a_1$ is $1$.

The doctor asked Chi to go to the hospital and do $m$, $(m \le 10^3)$ tests. He is can only go to the hospital on free days, but can do many different tests on the same day. For each test, Chi will need $z$, $(z < 10^9)$ sets of documents to be able to conclude the disease.

A document is a chart that compares body indicators between any two days to monitor health conditions. A valid document requires that the gap between two test dates must be between $x$ and $y$ days $(1 < x \le y < 10^9)$ and the hospital only needs enough documents before concluding the disease. without prohibiting the time of any two sets of documents to overlap for a period of time. That means you can use data from a past medical examination day to create multiple sets of documents for the same test.

Input

The first line contains one integer $n, m$.

The second line contains $n$ increasing non-negative natural numbers represent the upcoming free days of Chi.

The next $m$ lines, each line includes 3 numbers: $x, y, z$. With $x, y, z$ is the requirement that Chi must complete $z$ sets of documents, with the condition of each set being the gap between $2$ days in the range of $x - y$ days.

Output

Print the earliest date that Chi will complete all tests. If that date is not available, return $-1$.

Note

In the first example: Chi will make $2$ sets of documents. $1$ set is to compare day $4$ compared to day $1$. $1$ set is to compare day $4$ compared to day $2$. Complete at the earliest on day $4$.

In the second example: Test $1$ will be completed on day $18$ at the earliest. Test $2$ will be completed on the day $16$ at the earliest. Print out $18$.

Note (VIETNAMSON)

  • Problem of The 2023 ICPC Vietnam Central Provincial Programming Contest (Hue University of Sciences, September 22, 2023). The statement and samples are kept verbatim from the original PDF.
  • In the original PDF all samples are printed in one table; here they are split into separate samples (content unchanged).
  • The PDF writes $1 < a_i$ although sample 1 contains $a_1 = 1$; "The first line contains one integer $n, m$", "he is suffered", "He is can only" are kept verbatim. "In the range of $x - y$ days" means the gap $d$ satisfies $x \le d \le y$ (both ends included, as in sample 1).
  • A test is completed on the free day $a_j$ when the number of pairs of free days (an earlier day $a_i$, a day $a_j$) with $x \le a_j - a_i \le y$ and $a_j$ not later than that day reaches $z$; a day may be used in many pairs. The answer is the maximum over the tests (or $-1$ if some test can never be completed). The official tests (VNOJ) are consistent with counting, for each day $a_j$, every free day $a_i$ with $a_j - y \le a_i \le a_j - x$; under the stated constraints this is exactly the pair count above. Tests on oj.vietnamson.com follow the stated constraints (distinct increasing days, $x > 1$, $z \ge 1$).
  • Python on oj.vietnamson.com: time limit 3 s, memory limit 1 GB.
  • Pure Python cannot pass this time limit (as in the real contest); the Python code is for reference - use C++ in contests.
Ví dụ / Sample 1
Input
5 1
1 2 4 5 7
2 3 2

Output
4
Ví dụ / Sample 2
Input
8 2
1 4 6 8 12 16 18 20
2 3 4
4 5 4

Output
18

Ý tưởng và lời giải chi tiết

Phần phân tích ý tưởng, các cách giải từ cơ bản đến tối ưu kèm code C++/Python và chấm thử trên VIETNAMSON OJ dành cho học viên đã đăng ký gói.

Em đã mua gói thì đăng nhập để xem. Chưa có gói thì bấm Xem giới thiệu gói để biết gói gồm những đề nào, bao nhiêu bài, học phí và ưu đãi; muốn đăng ký thì nhắn Zalo thầy Sơn.
Tự học thuật toán: khó ở đâu?
Đọc xong một bài giải là bước đầu. Phần lớn học sinh tự học sẽ gặp những trở ngại sau:
  • Không biết học gì trước, học gì sau, học lan man nên kiến thức bị hổng.
  • Bí bài không ai gỡ, ngồi cả buổi vẫn không ra rồi bỏ cuộc.
  • Code chạy được nhưng không đủ điểm, không biết sai ở đâu, chậm ở đâu.
  • Không có đề thi thử và người chấm, nên không biết mình đang ở mức nào.
  • Dễ nản giữa chừng khi không có ai theo sát.
Học cùng thầy Nam Sơn, các trở ngại đó được gỡ bỏ:
  • Lộ trình 111 buổi video sắp sẵn từ dòng code đầu tiên đến đề HSG thật của các tỉnh.
  • Thầy trực tiếp gỡ bí, chữa bài cho từng em qua nhóm Zalo riêng.
  • Hệ thống chấm bài tự động 24/7 và thi thử HSG hằng tuần để biết chính xác mình đang ở đâu.
  • Đóng học phí một lần, thầy kèm lâu dài: từ thi chuyên Tin đến HSG quốc gia và cả khi lên đại học.