Xếp hàng mua vé

View as PDF

Submit solution


Points: 0.05 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Problem source:
VNOI Marathon '08 - Practice Round
Problem type
Allowed languages
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch

Có ~N~ người sắp hàng mua vé dự buổi hoà nhạc. Ta đánh số họ từ ~1~ đến ~N~ theo thứ tự đứng trong hàng. Mỗi người cần mua một vé, song người bán vé được phép bán cho mỗi người tối đa hai vé. Vì thế, một số người có thể rời hàng và nhờ người đứng trước mình mua hộ vé. Biết ~t_{i}~ là thời gian cần thiết để người ~i~ mua xong vé cho mình. Nếu người ~i+1~ rời khỏi hàng và nhờ người ~i~ mua hộ vé thì thời gian để người thứ ~i~ mua được vé cho cả hai người là ~r_{i}~.

Yêu cầu: Xác định xem những người nào cần rời khỏi hàng và nhờ người đứng trước mua hộ vé để tổng thời gian phục vụ bán vé là nhỏ nhất.

Input

  • Dòng đầu tiên chứa số ~N~ ~(1 \leq N \leq 60000)~.
  • Dòng thứ hai ghi ~N~ số nguyên dương ~t_{1}~, ~t_{2}~, ..., ~t_{N}~. ~(1 \leq t_{i} \leq 30000)~
  • Dòng thứ ba ghi ~N-1~ số nguyên dương ~r_{1}~, ~r_{2}~, ..., ~r_{N-1}~. ~(1 \leq r_{i} \leq 30000)~

Output

In ra tổng thời gian phục vụ nhỏ nhất.

Sample Input

5
2 5 7 8 4
4 9 10 10

Sample Output

18

Comments

Please read the guidelines before commenting.



  • 1
    hanguyen140210  commented on Jan. 6, 2026, 7:16 a.m.

    include<bits/stdc++.h>

    using namespace std;

    define vec vector

    define ll long long

    int i,j; int main(){ iosbase::syncwith_stdio(0); cin.tie(0);cout.tie(0); int n;cin>>n; vec<int>a(n+1),b(n+1),dp(n+1); for(i=1;i<=n;i++)cin>>a[i]; for(i=1;i<n;i++)cin>>b[i]; dp[0]=0; dp[1]=a[1]; for(i=2;i<=n;i++){ dp[i]=min( dp[i-1]+a[i], dp[i-2]+b[i-1] ); } cout<<dp[n]; }


  • 1
    vominhmanh10  commented on Nov. 1, 2025, 3:15 a.m. edit 3

    quy hoạch động thôi, trạng thái là tới người chỉ số i
    nếu mua cho mình, ghép với người trước đó dp[i - 1] + a[i]
    nếu mua luôn cho người ta, tính người trước đó nữa dp[i - 2]) + b[i]
    => dp[i] = min(dp[i - 1] + a[i], dp[i - 2] + b[i])

    import sys
    inf = float('inf')
    def main(n, a, b):
        dp = [0] * n
        dp[0] = a[0]
        dp[1] = min(a[0] + a[1], b[1])
        for i in range(2, n):
            dp[i] = min(dp[i - 1] + a[i], dp[i - 2] + b[i])
        return dp[n - 1]
    
    input = sys.stdin.readline
    n = int(input())
    a = list(map(int, input().split()))
    b = [0] + list(map(int, input().split()))
    print(main(n, a, b))
    

  • -5
    ngphong15082001  commented on Aug. 3, 2025, 10:15 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -13
    vutuankiet  commented on July 20, 2025, 2:55 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -33
    khiemgia1105  commented on Oct. 4, 2024, 7:48 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -9
    Huy_inIT  commented on Aug. 8, 2024, 12:30 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -12
    MalazeKL  commented on July 2, 2024, 6:04 p.m. edit 6

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -34
    hieuz08  commented on June 11, 2024, 2:12 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • -6
      nhanphamj  commented on July 27, 2024, 2:24 p.m.

      This comment is hidden due to too much negative feedback. Show it anyway.


  • -16
    a3k27_13_nguyenvantai  commented on March 30, 2024, 3:48 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -9
    thanhhoang  commented on Jan. 23, 2024, 7:01 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • -6
      thanhhoang  commented on Jan. 23, 2024, 7:02 p.m.

      This comment is hidden due to too much negative feedback. Show it anyway.


  • -22
    Cuunon311  commented on Oct. 8, 2023, 4:14 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -43
    kevininyourarea  commented on April 14, 2023, 1:46 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -40
    NguyenDongAnh  commented on Sept. 9, 2022, 2:29 p.m. edited

    This comment is hidden due to too much negative feedback. Show it anyway.


  • 30
    sickboy1368  commented on Jan. 11, 2022, 6:46 a.m.

    quy hoạch động so sánh ( f[i-1] + t[i] ) với ( f[i-2] + r[i-1] )


  • -385
    ltphuc  commented on April 20, 2021, 3:45 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • -154
      nngovannhhungg  commented on Jan. 15, 2022, 7:19 a.m.

      This comment is hidden due to too much negative feedback. Show it anyway.


    • -170
      manhtuan22007  commented on Nov. 27, 2021, 12:54 p.m.

      This comment is hidden due to too much negative feedback. Show it anyway.


    • -167
      ekhoavvdd  commented on Nov. 22, 2021, 4:31 p.m.

      This comment is hidden due to too much negative feedback. Show it anyway.