Pagini recente » Cod sursa (job #2145925) | Cod sursa (job #662471) | Cod sursa (job #1866754) | Cod sursa (job #1824586) | Cod sursa (job #1951513)
#include<bits/stdc++.h>
using namespace std;
class Reader
{
public:
Reader() {}
Reader(const char *file_name)
{
input_file = fopen(file_name, "r");
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
inline Reader &operator >>(int &n)
{
while(buffer[cursor] < '0' || buffer[cursor] > '9')
{
advance();
}
n = 0;
while('0' <= buffer[cursor] && buffer[cursor] <= '9')
{
n = n * 10 + buffer[cursor] - '0';
advance();
}
return *this;
}
inline Reader &operator >>(char &n)
{
while(!((buffer[cursor] >= 'a' && buffer[cursor] <= 'z') || (buffer[cursor] >= 'A' && buffer[cursor] <= 'Z') ))
{
advance();
}
if( (buffer[cursor] >= 'a' && buffer[cursor] <= 'z') || (buffer[cursor] >= 'A' && buffer[cursor] <= 'Z'))
{
n=buffer[cursor] ;
advance();
}
return *this;
}
private:
FILE *input_file;
static const int SIZE = 1 << 17;
int cursor;
char buffer[SIZE];
inline void advance()
{
++ cursor;
if(cursor == SIZE)
{
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
}
};
Reader f("rucsac.in");
ofstream g("rucsac.out");
int DP[10003];
int N,G,W,P;
int main()
{
f>>N>>G;
for(int i=1;i<=N;i++)
{
f>>W>>P;
for(int j=G-W;j>=0;j--)
if(DP[j]+P>DP[j+W]) DP[j+W]=DP[j]+P;
}
g<<DP[G];
}