Skip to content

Instantly share code, notes, and snippets.

@ctylim
Created September 1, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctylim/3c905234ff04097b5da2 to your computer and use it in GitHub Desktop.
Save ctylim/3c905234ff04097b5da2 to your computer and use it in GitHub Desktop.
yukicoder No.247
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
using namespace std;
int inputValue(){
int a;
cin >> a;
return a;
};
void inputArray(int * p, int a){
rep(i, a){
cin >> p[i];
}
};
void inputVector(vector<int> * p, int a){
rep(i, a){
int input;
cin >> input;
p -> push_back(input);
}
}
template <typename T>
void output(T a, int precision) {
if(precision > 0){
cout << setprecision(precision) << a << "\n";
}
else{
cout << a << "\n";
}
}
int dp[1000000] = {-1};
int main(int argc, const char * argv[]) {
// source code
int C = inputValue();
int N = inputValue();
vector<int> a;
rep(i, N){
a.push_back(inputValue());
}
dp[0] = 0;
repd(i, 1, C + 1){
int m = 10000000;
rep(j, N){
if (i >= a[j]) {
m = min(m, dp[i - a[j]] + 1);
}
}
dp[i] = m;
}
output((dp[C] == 10000000) ? -1 : dp[C], 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment