Gym 100703F Game of words 动态规划

来源:互联网 发布:python 调用百度搜索 编辑:程序博客网 时间:2024/05/19 12:18

F. Game of words
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

— But how can all of them be acquainted, if no one wants to see anyone else? — Princess still did not find Dragon's plan feasible.

— Not quite, — Dragon circumstantiated, — No one wants an unplanned contact. But, for example, they all come to intellectual competitions eagerly. There are games where teams consist of two people...

Dragon told Princess that qualification competition for Word Game begins in Castles Valley soon. Teams scored enough points participate in the next round of the competition. If several teams score an equal number of points, the team spent less time is placed higher.

Let us describe the rules of qualification game.

Game master has m cards in a pack. Only one word is written on each card. Players take turns. Before the game they agree which of them makes the first move and inform game master about it. Let us call the player who moves first The First Player, and call The Second Player another.

At the beginning, game master gives The First Player a card. The First Player tries to explain the word on the card without naming the word to The Second Player. When the explanation finished, The Second Player says his guess. If The Second Player's answer is the same as the word on the card, the team scores a point. Then, the game master gives The Second Player a card, and The Second Player tries to explain the word on the card to The First Player. Then a card is given to The First Player again... The game continues until cards run out.

But there is an important additional rule in this season. Player must use a terminology of only one subject area when he explains his word. Moreover, a terminology of each subject area should be used only once in qualification game. The n (n ≥ m) subject areas are specified in the rules.

A pair of players — X and Y — has prepared for a qualification game carefully. They believe that each of them can guess any word regardless of a terminology used for explanations. It is simply a matter of time.

After a series of trainings, they studied the time needed to guess the word that was explained with each terminology to each of them.

Now they want to know, what minimum possible time is required for them to score m points. Your tasks is to compute the time.

Input

The first line contains integers m and n (1 ≤ m ≤ n ≤ 400) — the number of cards in a pack and the number of subject areas, which terminologies can be used for explanations.

The second line contains n integers p1, p2, ..., pn, where pj (1 ≤ pj ≤ 106) — the time player X needs to guess a word, if he listens to explanation of this word with jth subject area terminology.

The third line contains n integers q1, q2, ..., qn, where qk (1 ≤ qk ≤ 106) — the time player Y needs to guess a word, if he listens explanation of this word with kth subject area terminology.

Output

Print integer — the minimum possible time, which players need for explanation of m words (in accordance to game rules) in the first line. Players can choose, who of them makes the first step.

Sample test(s)
input
3 55 4 7 6 28 3 5 4 2
output
9
input
4 42 4 6 81 4 6 7
output
18
Note

In the first sample players should play the following way. The first step: player X explains a word to Y using the terms of the 2nd subject area, and Y guesses this word after 3 time units. The second step: player Y uses terms of 5th subject area for explanation of a word, and X guesses this word after 2 time units. The third step: player X explains yet another word using terms of the 4th subject area, and Yguesses this word after 4 time units.


题目链接:http://codeforces.com/gym/100703/problem/F

题目大意:m个游戏中共选出n个游戏玩,其中,已知m个游戏X和Y玩需要的时间,问,X和Y各玩m/2个游戏所需要的最少总时间?

思路:第i层循环中的,dp[j][k]表示前i个游戏,X玩了j个,Y玩了k个,所需要的最小时间。总时间复杂度O(n^2*m),需要注意的是,由于此处选择了二维dp数组,所以,为了不影响前驱数据,j和k所在的循环应该倒着枚举。当然也可以采用滚动数组的方式等。

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <set>using namespace std;typedef long long LL;#define MAXN 500int dp[MAXN][MAXN];int n, m;int a[MAXN], b[MAXN];int main() {    scanf("%d%d", &m, &n);    memset(dp, 0x3f, sizeof dp);    for(int i = 1; i <= n; i++) {        scanf("%d", &a[i]);    }    for(int i = 1; i <= n; i++) {        scanf("%d", &b[i]);    }    int s = 0;    dp[0][0] = 0;    for(int i = 1; i <= n; i++) {        for(int j = i; j >= 0; j--) {            for(int k = i - j; k >= 0; k--) {                dp[j + 1][k] = min(dp[j + 1][k], dp[j][k] + a[i]);                dp[j][k + 1] = min(dp[j][k + 1], dp[j][k] + b[i]);            }        }    }    int ans = min(dp[(m + 1)/2][m / 2], dp[m / 2][(m + 1)/2]);    printf("%d\n", ans);    return 0;}




0 0