QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#286928#5592. Family Visitsmfshi03WA 15ms9164kbPython31.1kb2023-12-19 07:18:312023-12-19 07:18:32

Judging History

你现在查看的是最新测评结果

  • [2023-12-19 07:18:32]
  • 评测
  • 测评结果:WA
  • 用时:15ms
  • 内存:9164kb
  • [2023-12-19 07:18:31]
  • 提交

answer

# Attempt 2 
n, d = map(int, input().split())
days = [list(map(int, input().split())) for i in range(n)]
visits = [int(input()) for i in range(d)]

dp = dict()

sol = 0

def solve(start, n):
    #print("start", start, n)
    s = n - start + 1 
    for k in range(s+1):
        for i, day in reversed(list(enumerate(days[start:n+1], 1))):
            if i == s:
                if k == 0:
                    dp[(i, k)] = day[1] - day[0]
                else:
                    dp[(i, k)] = -day[0]
            else:
                if k > 0:
                    dp[(i, k)] = max(dp[(i + 1, k)] - day[0], dp[(i + 1, k - 1)] + day[1] - day[0])
                else:
                    dp[(i, k)] = dp[(i + 1, k)] - day[0]
        
        #print(i, k, dp[(i, k)])
        if dp[(1, k)] >= 0:
            return k + 1

    return float("-inf")

start = 0
np = False
for v in visits:
    add = solve(start, v - 1)
    start = v
    dp = dict()
    if add < 0:
        np = True
        break

    sol += add


if np:
    print(-1)
else:
    print(sol)




        



Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 15ms
memory: 9040kb

input:

6 2
1 2
2 1
1 4
3 2
3 6
2 3
3
6

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 9ms
memory: 9148kb

input:

10 5
12 10
0 2
7 1
1 8
3 4
3 4
2 3
1 2
10 1
7 5
2
4
5
6
8

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 15ms
memory: 9164kb

input:

10 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
10

output:

10

result:

ok single line: '10'

Test #4:

score: 0
Accepted
time: 14ms
memory: 9044kb

input:

3 1
10 10
10 10
10 0
2

output:

2

result:

ok single line: '2'

Test #5:

score: 0
Accepted
time: 10ms
memory: 9044kb

input:

12 1
1 1
1 1
1 1
0 4
1 1
1 1
1 1
0 6
1 1
1 1
1 1
0 4
12

output:

2

result:

ok single line: '2'

Test #6:

score: 0
Accepted
time: 14ms
memory: 9016kb

input:

6 1
1 0
1 0
1 2
1 0
1 0
1 2
6

output:

-1

result:

ok single line: '-1'

Test #7:

score: -100
Wrong Answer
time: 10ms
memory: 9064kb

input:

9 1
1 0
1 0
1 9
1 0
1 0
1 2
1 0
1 0
1 3
9

output:

2

result:

wrong answer 1st lines differ - expected: '-1', found: '2'